From 92cafd88510cc6a0908fab43d58837acca68773f Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 15 Mar 2022 19:50:17 +0800 Subject: [PATCH] checker: check invalid 'mut' keyword in infix expr (#13742) --- vlib/v/checker/checker.v | 10 +++++++++- vlib/v/checker/tests/invalid_mut.out | 14 ++++++++++++++ .../tests/invalid_mut.vv} | 3 +++ vlib/v/parser/tests/unnecessary_mut.out | 7 ------- 4 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 vlib/v/checker/tests/invalid_mut.out rename vlib/v/{parser/tests/unnecessary_mut.vv => checker/tests/invalid_mut.vv} (65%) delete mode 100644 vlib/v/parser/tests/unnecessary_mut.out diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ccbb01337f..9c990ada5f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -594,12 +594,20 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { match mut node.left { ast.Ident, ast.SelectorExpr { if node.left.is_mut { - c.error('remove unnecessary `mut`', node.left.mut_pos) + c.error('the `mut` keyword is invalid here', node.left.mut_pos) } } else {} } } + match mut node.right { + ast.Ident, ast.SelectorExpr { + if node.right.is_mut { + c.error('the `mut` keyword is invalid here', node.right.mut_pos) + } + } + else {} + } eq_ne := node.op in [.eq, .ne] // Single side check // Place these branches according to ops' usage frequency to accelerate. diff --git a/vlib/v/checker/tests/invalid_mut.out b/vlib/v/checker/tests/invalid_mut.out new file mode 100644 index 0000000000..9747d4e444 --- /dev/null +++ b/vlib/v/checker/tests/invalid_mut.out @@ -0,0 +1,14 @@ +vlib/v/checker/tests/invalid_mut.vv:3:5: error: the `mut` keyword is invalid here + 1 | fn main() { + 2 | mut x := 0 + 3 | if mut x == 0 { + | ~~~ + 4 | println(true) + 5 | } +vlib/v/checker/tests/invalid_mut.vv:6:10: error: the `mut` keyword is invalid here + 4 | println(true) + 5 | } + 6 | if 0 == mut x { + | ~~~ + 7 | println(true) + 8 | } diff --git a/vlib/v/parser/tests/unnecessary_mut.vv b/vlib/v/checker/tests/invalid_mut.vv similarity index 65% rename from vlib/v/parser/tests/unnecessary_mut.vv rename to vlib/v/checker/tests/invalid_mut.vv index 6c7a470ed5..d4b27b2ca9 100644 --- a/vlib/v/parser/tests/unnecessary_mut.vv +++ b/vlib/v/checker/tests/invalid_mut.vv @@ -3,5 +3,8 @@ fn main() { if mut x == 0 { println(true) } + if 0 == mut x { + println(true) + } _ = x } diff --git a/vlib/v/parser/tests/unnecessary_mut.out b/vlib/v/parser/tests/unnecessary_mut.out deleted file mode 100644 index 4399c9f164..0000000000 --- a/vlib/v/parser/tests/unnecessary_mut.out +++ /dev/null @@ -1,7 +0,0 @@ -vlib/v/parser/tests/unnecessary_mut.vv:3:5: error: remove unnecessary `mut` - 1 | fn main() { - 2 | mut x := 0 - 3 | if mut x == 0 { - | ~~~ - 4 | println(true) - 5 | } \ No newline at end of file