From 3be78d6777caee18a3ba061f19c5f88a557d7882 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 12 Mar 2021 12:17:37 +0000 Subject: [PATCH] parser: require anonymous fn to use `_` for unused parameters (#9262) Fixes a C error with gcc. --- vlib/v/parser/fn.v | 3 +++ vlib/v/parser/tests/anon_unused_param.out | 4 ++++ vlib/v/parser/tests/anon_unused_param.vv | 2 ++ 3 files changed, 9 insertions(+) create mode 100644 vlib/v/parser/tests/anon_unused_param.out create mode 100644 vlib/v/parser/tests/anon_unused_param.vv diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 133049e95..e8dcfea53 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -544,6 +544,9 @@ fn (mut p Parser) anon_fn() ast.AnonFn { // TODO generics args, _, is_variadic := p.fn_args() for arg in args { + if arg.name.len == 0 { + p.error_with_pos('use `_` to name an unused parameter', arg.pos) + } p.scope.register(ast.Var{ name: arg.name typ: arg.typ diff --git a/vlib/v/parser/tests/anon_unused_param.out b/vlib/v/parser/tests/anon_unused_param.out new file mode 100644 index 000000000..a12769b30 --- /dev/null +++ b/vlib/v/parser/tests/anon_unused_param.out @@ -0,0 +1,4 @@ +vlib/v/parser/tests/anon_unused_param.vv:1:9: error: use `_` to name an unused parameter + 1 | _ = fn (int){} + | ~~~ + 2 | diff --git a/vlib/v/parser/tests/anon_unused_param.vv b/vlib/v/parser/tests/anon_unused_param.vv new file mode 100644 index 000000000..c285cea3c --- /dev/null +++ b/vlib/v/parser/tests/anon_unused_param.vv @@ -0,0 +1,2 @@ +_ = fn (int){} + -- 2.39.5