checker: add check for call expr in map/filter (#9559)

pull/9562/head
Daniel Däschle 2021-04-02 01:56:51 +02:00 committed by GitHub
parent 7385f8e56b
commit d8efe249ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 0 deletions

7
main.v 100644
View File

@ -0,0 +1,7 @@
fn main() {
list := [1,2,3].filter(stringsss(it))
}
fn stringsss(arg int) string {
return ''
}

View File

@ -1409,6 +1409,13 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, call_exp
}
}
}
ast.CallExpr {
if is_map && arg_expr.return_type == table.void_type {
c.error('type mismatch, `$arg_expr.name` does not return anything', arg_expr.pos)
} else if !is_map && arg_expr.return_type != table.bool_type {
c.error('type mismatch, `$arg_expr.name` must return a bool', arg_expr.pos)
}
}
else {}
}
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/filter_func_return_nonbool_err.vv:2:25: error: type mismatch, `stringsss` must return a bool
1 | fn main() {
2 | list := [1,2,3].filter(stringsss(it))
| ~~~~~~~~~~~~~
3 | }
4 |

View File

@ -0,0 +1,7 @@
fn main() {
list := [1,2,3].filter(stringsss(it))
}
fn stringsss(arg int) string {
return ''
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/map_func_void_return_err.vv:2:22: error: type mismatch, `voids` does not return anything
1 | fn main() {
2 | list := [1,2,3].map(voids(it))
| ~~~~~~~~~
3 | }
4 |

View File

@ -0,0 +1,7 @@
fn main() {
list := [1,2,3].map(voids(it))
}
fn voids(arg int) {
println(arg)
}