Skip to content

Commit

Permalink
checker: disallow void return value lambdas for map
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Mar 13, 2024
1 parent 80d20af commit 71dcc4b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2739,6 +2739,14 @@ 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)
}
}
ast.LambdaExpr {
if arg_expr.expr is ast.CallExpr {
if 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 {}
}
}
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/tests/map_lambda_void_return_err.out
Original file line number Diff line number Diff line change
@@ -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))
| ~~~~~~~~~~
2 changes: 2 additions & 0 deletions vlib/v/checker/tests/map_lambda_void_return_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
arr := ['a', 'b', 'c']
arr.map(|x| println(x))

0 comments on commit 71dcc4b

Please sign in to comment.