checker: disallow void return value lambdas in array.map method calls (#21011)

This commit is contained in:
Swastik Baranwal 2024-03-13 17:51:57 +05:30 committed by GitHub
parent fee184b67f
commit b276aaccf9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 0 deletions

View file

@ -2739,6 +2739,13 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast
c.error('array append cannot be used in an expression', arg_expr.pos) c.error('array append cannot be used in an expression', arg_expr.pos)
} }
} }
ast.LambdaExpr {
if arg_expr.expr is ast.CallExpr && is_map
&& arg_expr.expr.return_type in [ast.void_type, 0] {
c.error('type mismatch, `${arg_expr.expr.name}` does not return anything',
arg_expr.expr.pos)
}
}
else {} else {}
} }
} }

View file

@ -0,0 +1,4 @@
vlib/v/checker/tests/map_lambda_void_return_err.vv:2:13: error: type mismatch, `println` does not return anything
1 | arr := ['a', 'b', 'c']
2 | arr.map(|x| println(x))
| ~~~~~~~~~~

View file

@ -0,0 +1,2 @@
arr := ['a', 'b', 'c']
arr.map(|x| println(x))