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

checker: forbid var declaration in post statement of for loop (#17120)

This commit is contained in:
Makhnev Petr 2023-01-26 19:40:10 +04:00 committed by GitHub
parent 15c0a73740
commit 6365d9e070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View File

@ -14,7 +14,13 @@ fn (mut c Checker) for_c_stmt(node ast.ForCStmt) {
c.expr(node.cond)
if node.has_inc {
if node.inc is ast.AssignStmt {
for right in node.inc.right {
assign := node.inc
if assign.op == .decl_assign {
c.error('for loop post statement cannot be a variable declaration', assign.pos)
}
for right in assign.right {
if right is ast.CallExpr {
if right.or_block.stmts.len > 0 {
c.error('options are not allowed in `for statement increment` (yet)',

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/for_c_stmt_with_var_declaration_in_post_stmt.vv:1:26: error: for loop post statement cannot be a variable declaration
1 | for i := 100; i < 200; j := 100 {
| ~~
2 | println(j)
3 | }

View File

@ -0,0 +1,3 @@
for i := 100; i < 200; j := 100 {
println(j)
}