Skip to content

Commit 63eaced

Browse files
authored
checker: check argument mismatch of array.filter/all/any() (#14273)
1 parent 6da3004 commit 63eaced

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

vlib/v/checker/fn.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,11 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ ast.Type, node ast
17781778
c.error('type mismatch, `$arg_expr.name` must return a bool', arg_expr.pos)
17791779
}
17801780
}
1781+
ast.StringLiteral, ast.StringInterLiteral {
1782+
if !is_map {
1783+
c.error('type mismatch, should use e.g. `${node.name}(it > 2)`', arg_expr.pos)
1784+
}
1785+
}
17811786
else {}
17821787
}
17831788
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
vlib/v/checker/tests/array_filter_arg_mismatch_err.vv:17:20: error: type mismatch, should use e.g. `filter(it > 2)`
2+
15 | }]
3+
16 |
4+
17 | a1 := list.filter('it.value > 2')
5+
| ~~~~~~~~~~~~~~
6+
18 | println(a1)
7+
19 |
8+
vlib/v/checker/tests/array_filter_arg_mismatch_err.vv:20:17: error: type mismatch, should use e.g. `any(it > 2)`
9+
18 | println(a1)
10+
19 |
11+
20 | a2 := list.any('it.value > 2')
12+
| ~~~~~~~~~~~~~~
13+
21 | println(a2)
14+
22 |
15+
vlib/v/checker/tests/array_filter_arg_mismatch_err.vv:23:17: error: type mismatch, should use e.g. `all(it > 2)`
16+
21 | println(a2)
17+
22 |
18+
23 | a3 := list.all('it.value > 2')
19+
| ~~~~~~~~~~~~~~
20+
24 | println(a3)
21+
25 | }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module main
2+
3+
pub struct Row {
4+
pub mut:
5+
value int
6+
}
7+
8+
fn main() {
9+
mut list := [Row{
10+
value: 1
11+
}, Row{
12+
value: 2
13+
}, Row{
14+
value: 3
15+
}]
16+
17+
a1 := list.filter('it.value > 2')
18+
println(a1)
19+
20+
a2 := list.any('it.value > 2')
21+
println(a2)
22+
23+
a3 := list.all('it.value > 2')
24+
println(a3)
25+
}

0 commit comments

Comments
 (0)