checker: check argument mismatch of array.filter/all/any() (#14273)

yuyi 2022-05-03 21:56:18 +08:00 committed by Jef Roosens
parent 0669e5952b
commit f9c4cbdb15
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 51 additions and 0 deletions

View File

@ -1778,6 +1778,11 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast
c.error('type mismatch, `$arg_expr.name` must return a bool', arg_expr.pos) c.error('type mismatch, `$arg_expr.name` must return a bool', arg_expr.pos)
} }
} }
ast.StringLiteral, ast.StringInterLiteral {
if !is_map {
c.error('type mismatch, should use e.g. `${node.name}(it > 2)`', arg_expr.pos)
}
}
else {} else {}
} }
} }

View File

@ -0,0 +1,21 @@
vlib/v/checker/tests/array_filter_arg_mismatch_err.vv:17:20: error: type mismatch, should use e.g. `filter(it > 2)`
15 | }]
16 |
17 | a1 := list.filter('it.value > 2')
| ~~~~~~~~~~~~~~
18 | println(a1)
19 |
vlib/v/checker/tests/array_filter_arg_mismatch_err.vv:20:17: error: type mismatch, should use e.g. `any(it > 2)`
18 | println(a1)
19 |
20 | a2 := list.any('it.value > 2')
| ~~~~~~~~~~~~~~
21 | println(a2)
22 |
vlib/v/checker/tests/array_filter_arg_mismatch_err.vv:23:17: error: type mismatch, should use e.g. `all(it > 2)`
21 | println(a2)
22 |
23 | a3 := list.all('it.value > 2')
| ~~~~~~~~~~~~~~
24 | println(a3)
25 | }

View File

@ -0,0 +1,25 @@
module main
pub struct Row {
pub mut:
value int
}
fn main() {
mut list := [Row{
value: 1
}, Row{
value: 2
}, Row{
value: 3
}]
a1 := list.filter('it.value > 2')
println(a1)
a2 := list.any('it.value > 2')
println(a2)
a3 := list.all('it.value > 2')
println(a3)
}