From f9c4cbdb15d23360086b93a47c54938f134ab2e3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 3 May 2022 21:56:18 +0800 Subject: [PATCH] checker: check argument mismatch of array.filter/all/any() (#14273) --- vlib/v/checker/fn.v | 5 ++++ .../tests/array_filter_arg_mismatch_err.out | 21 ++++++++++++++++ .../tests/array_filter_arg_mismatch_err.vv | 25 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 vlib/v/checker/tests/array_filter_arg_mismatch_err.out create mode 100644 vlib/v/checker/tests/array_filter_arg_mismatch_err.vv diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index d1072743c8..bfefab5319 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -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) } } + ast.StringLiteral, ast.StringInterLiteral { + if !is_map { + c.error('type mismatch, should use e.g. `${node.name}(it > 2)`', arg_expr.pos) + } + } else {} } } diff --git a/vlib/v/checker/tests/array_filter_arg_mismatch_err.out b/vlib/v/checker/tests/array_filter_arg_mismatch_err.out new file mode 100644 index 0000000000..c0fdf4b6f6 --- /dev/null +++ b/vlib/v/checker/tests/array_filter_arg_mismatch_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/array_filter_arg_mismatch_err.vv b/vlib/v/checker/tests/array_filter_arg_mismatch_err.vv new file mode 100644 index 0000000000..e3f0548c98 --- /dev/null +++ b/vlib/v/checker/tests/array_filter_arg_mismatch_err.vv @@ -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) +}