1
0
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:
yuyi 2023-03-27 00:10:06 +08:00 committed by GitHub
parent 130f35c776
commit f08b88223d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -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)
} }

View 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'
}