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:
@@ -14,7 +14,13 @@ fn (mut c Checker) for_c_stmt(node ast.ForCStmt) {
|
|||||||
c.expr(node.cond)
|
c.expr(node.cond)
|
||||||
if node.has_inc {
|
if node.has_inc {
|
||||||
if node.inc is ast.AssignStmt {
|
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 is ast.CallExpr {
|
||||||
if right.or_block.stmts.len > 0 {
|
if right.or_block.stmts.len > 0 {
|
||||||
c.error('options are not allowed in `for statement increment` (yet)',
|
c.error('options are not allowed in `for statement increment` (yet)',
|
||||||
|
|||||||
@@ -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 | }
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
for i := 100; i < 200; j := 100 {
|
||||||
|
println(j)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user