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

parser: check undefined variable in if guard (#16138)

This commit is contained in:
yuyi 2022-10-22 03:48:59 +08:00 committed by GitHub
parent b40f1828b2
commit ab78d5a7ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 1 deletions

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/undefined_ident_in_if_guard_err.vv:6:11: error: undefined variable: `re`
vlib/v/checker/tests/undefined_ident_in_if_guard_err_a.vv:6:11: error: undefined variable: `re`
4 |
5 | fn main() {
6 | if re := re.regex_opt('a') {

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/undefined_ident_in_if_guard_err_b.vv:3:17: error: undefined variable: `id`
1 | fn main(){
2 | items := {'a': 'A'}
3 | if id := items[id] {} else {}
| ~~
4 | }

View File

@ -0,0 +1,4 @@
fn main(){
items := {'a': 'A'}
if id := items[id] {} else {}
}

View File

@ -59,6 +59,10 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? {
p.check_undefined_variables(exprs, arg.expr)?
}
}
ast.IndexExpr {
p.check_undefined_variables(exprs, val.left)?
p.check_undefined_variables(exprs, val.index)?
}
ast.InfixExpr {
p.check_undefined_variables(exprs, val.left)?
p.check_undefined_variables(exprs, val.right)?
@ -155,6 +159,10 @@ fn (mut p Parser) check_undefined_variables_by_names(names []string, val ast.Exp
p.check_undefined_variables_by_names(names, arg.expr)?
}
}
ast.IndexExpr {
p.check_undefined_variables_by_names(names, val.left)?
p.check_undefined_variables_by_names(names, val.index)?
}
ast.InfixExpr {
p.check_undefined_variables_by_names(names, val.left)?
p.check_undefined_variables_by_names(names, val.right)?