Skip to content

Commit f08b882

Browse files
authored
checker: fix returning error in if expr (#17783)
1 parent 130f35c commit f08b882

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

vlib/v/checker/if.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
327327
node.pos)
328328
}
329329
}
330-
} else if !node.is_comptime {
330+
} else if !node.is_comptime && stmt !is ast.Return {
331331
c.error('`${if_kind}` expression requires an expression as the last statement of every branch',
332332
branch.pos)
333333
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct NotFoundError {
2+
Error
3+
}
4+
5+
fn get_username() !string {
6+
return NotFoundError{}
7+
}
8+
9+
fn print_username() !string {
10+
username := get_username() or {
11+
if err is NotFoundError {
12+
'test'
13+
} else {
14+
return err
15+
}
16+
}
17+
18+
println(username)
19+
return username
20+
}
21+
22+
fn test_return_err_in_if_expr() {
23+
ret := print_username()!
24+
assert ret == 'test'
25+
}

0 commit comments

Comments
 (0)