1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: extend check assigning mut reference to immutable var in nested parens (#18442)

This commit is contained in:
Turiiya 2023-06-15 01:07:25 +02:00 committed by GitHub
parent 77a1f5928f
commit 34163ae44b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 5 deletions

View File

@ -733,7 +733,11 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
c.inside_ref_lit = old_inside_ref_lit c.inside_ref_lit = old_inside_ref_lit
if right_node.op == .amp { if right_node.op == .amp {
expr := if right_node.right is ast.ParExpr { expr := if right_node.right is ast.ParExpr {
right_node.right.expr mut expr_ := right_node.right.expr
for mut expr_ is ast.ParExpr {
expr_ = expr_.expr
}
expr_
} else { } else {
right_node.right right_node.right
} }

View File

@ -1,7 +1,77 @@
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:11:12: warning: redundant parentheses are used
9 | fn foo() {
10 | mut a := &(a_char)
11 | mut b := &((a_char))
| ~~~~~~~~~~
12 | mut c := &((((a_char))))
13 | println(a)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:11:12: warning: redundant parentheses are used
9 | fn foo() {
10 | mut a := &(a_char)
11 | mut b := &((a_char))
| ~~~~~~~~~~
12 | mut c := &((((a_char))))
13 | println(a)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:12: warning: redundant parentheses are used
10 | mut a := &(a_char)
11 | mut b := &((a_char))
12 | mut c := &((((a_char))))
| ~~~~~~~~~~~~~~
13 | println(a)
14 | println(b)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:13: warning: redundant parentheses are used
10 | mut a := &(a_char)
11 | mut b := &((a_char))
12 | mut c := &((((a_char))))
| ~~~~~~~~~~~~
13 | println(a)
14 | println(b)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:14: warning: redundant parentheses are used
10 | mut a := &(a_char)
11 | mut b := &((a_char))
12 | mut c := &((((a_char))))
| ~~~~~~~~~~
13 | println(a)
14 | println(b)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:12: warning: redundant parentheses are used
10 | mut a := &(a_char)
11 | mut b := &((a_char))
12 | mut c := &((((a_char))))
| ~~~~~~~~~~~~~~
13 | println(a)
14 | println(b)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:13: warning: redundant parentheses are used
10 | mut a := &(a_char)
11 | mut b := &((a_char))
12 | mut c := &((((a_char))))
| ~~~~~~~~~~~~
13 | println(a)
14 | println(b)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:14: warning: redundant parentheses are used
10 | mut a := &(a_char)
11 | mut b := &((a_char))
12 | mut c := &((((a_char))))
| ~~~~~~~~~~
13 | println(a)
14 | println(b)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:10:11: error: `a_char` is immutable, cannot have a mutable reference to it vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:10:11: error: `a_char` is immutable, cannot have a mutable reference to it
8 | 8 |
9 | fn foo() { 9 | fn foo() {
10 | mut c := &(a_char) 10 | mut a := &(a_char)
| ^ | ^
11 | println(c) 11 | mut b := &((a_char))
12 | } 12 | mut c := &((((a_char))))
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:11:11: error: `a_char` is immutable, cannot have a mutable reference to it
9 | fn foo() {
10 | mut a := &(a_char)
11 | mut b := &((a_char))
| ^
12 | mut c := &((((a_char))))
13 | println(a)
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:12:11: error: `a_char` is immutable, cannot have a mutable reference to it
10 | mut a := &(a_char)
11 | mut b := &((a_char))
12 | mut c := &((((a_char))))
| ^
13 | println(a)
14 | println(b)

View File

@ -7,6 +7,10 @@ const a_char = MyChar{
} }
fn foo() { fn foo() {
mut c := &(a_char) mut a := &(a_char)
mut b := &((a_char))
mut c := &((((a_char))))
println(a)
println(b)
println(c) println(c)
} }