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

checker: check invalid variable (fix #15240) (#15260)

This commit is contained in:
yuyi 2022-07-30 19:27:28 +08:00 committed by GitHub
parent 0133ff2cbf
commit b6ce7cc198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -2756,6 +2756,9 @@ pub fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
} else {
typ = obj.expr.expr_type.clear_flag(.optional).clear_flag(.result)
}
} else if obj.expr is ast.EmptyExpr {
c.error('invalid variable `$node.name`', node.pos)
typ = ast.void_type
} else {
typ = c.expr(obj.expr)
}

View File

@ -0,0 +1,19 @@
vlib/v/checker/tests/invalid_variable_err.vv:2:5: warning: unused variable: `b`
1 | fn main() {
2 | a, b := c
| ^
3 | if a {
4 | }
vlib/v/checker/tests/invalid_variable_err.vv:2:7: error: assignment mismatch: 2 variable(s) 1 value(s)
1 | fn main() {
2 | a, b := c
| ~~
3 | if a {
4 | }
vlib/v/checker/tests/invalid_variable_err.vv:3:5: error: invalid variable `a`
1 | fn main() {
2 | a, b := c
3 | if a {
| ^
4 | }
5 | }

View File

@ -0,0 +1,5 @@
fn main() {
a, b := c
if a {
}
}