checker: check invalid 'mut' keyword in infix expr (#13742)

pull/13748/head
yuyi 2022-03-15 19:50:17 +08:00 committed by GitHub
parent 1d83ab6be1
commit 92cafd8851
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 8 deletions

View File

@ -594,12 +594,20 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
match mut node.left { match mut node.left {
ast.Ident, ast.SelectorExpr { ast.Ident, ast.SelectorExpr {
if node.left.is_mut { 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 {} 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] eq_ne := node.op in [.eq, .ne]
// Single side check // Single side check
// Place these branches according to ops' usage frequency to accelerate. // Place these branches according to ops' usage frequency to accelerate.

View File

@ -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 | }

View File

@ -3,5 +3,8 @@ fn main() {
if mut x == 0 { if mut x == 0 {
println(true) println(true)
} }
if 0 == mut x {
println(true)
}
_ = x _ = x
} }

View File

@ -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 | }