1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix 'return none' in void optional function (#16545)

This commit is contained in:
yuyi 2022-11-28 16:29:02 +08:00 committed by GitHub
parent 7d8c386721
commit d257e43932
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -93,10 +93,6 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
if (exp_is_optional
&& got_types_0_idx in [ast.none_type_idx, ast.error_type_idx, option_type_idx])
|| (exp_is_result && got_types_0_idx in [ast.error_type_idx, result_type_idx]) {
if got_types_0_idx == ast.none_type_idx && expected_type == ast.ovoid_type {
c.error('returning `none` in functions, that have a `?` result type is not allowed anymore, either `return error(message)` or just `return` instead',
node.pos)
}
return
}
if expected_types.len > 0 && expected_types.len != got_types.len {

View File

@ -0,0 +1,12 @@
fn test_optional_void() {
foo(22)?
assert true
}
fn foo(n int) ? {
if n > 0 {
println(n)
} else {
return none
}
}