From dd7ebf7fac599714ee0ba550564ef01823bce959 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 4 Jun 2020 21:20:43 +0800 Subject: [PATCH] parser: check function argument mutable syntax --- vlib/v/checker/tests/mut_args_warning.out | 5 +++++ vlib/v/checker/tests/mut_args_warning.vv | 5 +++++ vlib/v/parser/fn.v | 1 + 3 files changed, 11 insertions(+) create mode 100644 vlib/v/checker/tests/mut_args_warning.out create mode 100644 vlib/v/checker/tests/mut_args_warning.vv diff --git a/vlib/v/checker/tests/mut_args_warning.out b/vlib/v/checker/tests/mut_args_warning.out new file mode 100644 index 0000000000..64f4802893 --- /dev/null +++ b/vlib/v/checker/tests/mut_args_warning.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/mut_args_warning.v:1:8: warning: use `mut f Foo` instead of `f mut Foo` + 1 | fn f(x mut []int) { x[0] = 1 } + | ~~~ + 2 | fn main() { + 3 | mut x := [0] diff --git a/vlib/v/checker/tests/mut_args_warning.vv b/vlib/v/checker/tests/mut_args_warning.vv new file mode 100644 index 0000000000..c29b27d455 --- /dev/null +++ b/vlib/v/checker/tests/mut_args_warning.vv @@ -0,0 +1,5 @@ +fn f(x mut []int) { x[0] = 1 } +fn main() { + mut x := [0] + f(mut x) +} diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 23e38e6d09..ece7aaaf94 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -415,6 +415,7 @@ fn (mut p Parser) fn_args() ([]table.Arg, bool) { } if p.tok.kind == .key_mut { // TODO remove old syntax + p.warn_with_pos('use `mut f Foo` instead of `f mut Foo`', p.tok.position()) is_mut = true } if p.tok.kind == .ellipsis {