checker: fix error for returning optional (#13902)

pull/13911/head
yuyi 2022-04-02 22:25:01 +08:00 committed by GitHub
parent af79c1e6ef
commit 0bf0c73a49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -148,7 +148,7 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) {
if exp_is_optional && node.exprs.len > 0 {
expr0 := node.exprs[0]
if expr0 is ast.CallExpr {
if expr0.or_block.kind == .propagate {
if expr0.or_block.kind == .propagate && node.exprs.len == 1 {
c.error('`?` is not needed, use `return ${expr0.name}()`', expr0.pos)
}
}

View File

@ -0,0 +1,14 @@
fn func1() ?int {
return 0
}
fn func2() ?(int, int) {
return func1() ?, 1
}
fn test_return_optional() ? {
a, b := func2() ?
println('$a, $b')
assert a == 0
assert b == 1
}