checker: ignore last semicolon on or-expr (#21361)

This commit is contained in:
Felipe Pena 2024-04-29 07:16:55 -03:00 committed by GitHub
parent f33b712e84
commit f2f7fc3295
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View File

@ -1308,7 +1308,8 @@ fn (mut c Checker) check_or_expr(node ast.OrExpr, ret_type ast.Type, expr_return
// allow `f() or {}`
return
}
mut last_stmt := node.stmts.last()
mut valid_stmts := node.stmts.filter(it !is ast.SemicolonStmt)
mut last_stmt := if valid_stmts.len > 0 { valid_stmts.last() } else { node.stmts.last() }
c.check_or_last_stmt(mut last_stmt, ret_type, expr_return_type.clear_option_and_result())
}

View File

@ -0,0 +1,11 @@
fn foo() !int {
return 0
}
fn test_main() {
mut x := 1
// vfmt off
x = foo() or { panic("failed"); }
// vfmt on
println('x=${x}')
}