mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix returning error in if expr (#17783)
This commit is contained in:
parent
130f35c776
commit
f08b88223d
@ -327,7 +327,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
|
|||||||
node.pos)
|
node.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if !node.is_comptime {
|
} else if !node.is_comptime && stmt !is ast.Return {
|
||||||
c.error('`${if_kind}` expression requires an expression as the last statement of every branch',
|
c.error('`${if_kind}` expression requires an expression as the last statement of every branch',
|
||||||
branch.pos)
|
branch.pos)
|
||||||
}
|
}
|
||||||
|
25
vlib/v/tests/return_error_in_if_expr_test.v
Normal file
25
vlib/v/tests/return_error_in_if_expr_test.v
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
struct NotFoundError {
|
||||||
|
Error
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_username() !string {
|
||||||
|
return NotFoundError{}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_username() !string {
|
||||||
|
username := get_username() or {
|
||||||
|
if err is NotFoundError {
|
||||||
|
'test'
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println(username)
|
||||||
|
return username
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_return_err_in_if_expr() {
|
||||||
|
ret := print_username()!
|
||||||
|
assert ret == 'test'
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user