mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
checker: disallow void return value lambdas in array.map method calls (#21011)
This commit is contained in:
parent
fee184b67f
commit
b276aaccf9
3 changed files with 13 additions and 0 deletions
|
@ -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 {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
vlib/v/checker/tests/map_lambda_void_return_err.out
Normal file
4
vlib/v/checker/tests/map_lambda_void_return_err.out
Normal 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))
|
||||||
|
| ~~~~~~~~~~
|
2
vlib/v/checker/tests/map_lambda_void_return_err.vv
Normal file
2
vlib/v/checker/tests/map_lambda_void_return_err.vv
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
arr := ['a', 'b', 'c']
|
||||||
|
arr.map(|x| println(x))
|
Loading…
Add table
Add a link
Reference in a new issue