diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index e72020c521..c5ffcd7af2 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -178,9 +178,15 @@ fn (f mut Fmt) stmt(node ast.Stmt) { f.writeln('}\n') } ast.ForInStmt { - f.write('for $it.key_var') + f.write('for ') + if it.key_var != '' { + f.write(it.key_var) + } if it.val_var != '' { - f.write(', $it.val_var') + if it.key_var != '' { + f.write(', ') + } + f.write(it.val_var) } f.write(' in ') f.expr(it.cond) diff --git a/vlib/v/fmt/tests/loops_expected.vv b/vlib/v/fmt/tests/loops_expected.vv new file mode 100644 index 0000000000..f623d79983 --- /dev/null +++ b/vlib/v/fmt/tests/loops_expected.vv @@ -0,0 +1,17 @@ +fn for_in_loop() { + for item in arr { + println(item) + } +} + +fn for_in_loop_with_counter() { + for i, item in arr { + println(item) + } +} + +fn for_in_loop_with_index_expr() { + for i in 0 .. 10 { + println(i) + } +} diff --git a/vlib/v/fmt/tests/loops_input.vv b/vlib/v/fmt/tests/loops_input.vv new file mode 100644 index 0000000000..c4fd2c19dd --- /dev/null +++ b/vlib/v/fmt/tests/loops_input.vv @@ -0,0 +1,17 @@ +fn for_in_loop() { + for item in arr { + println(item) + } +} + +fn for_in_loop_with_counter() { + for i, item in arr { + println(item) + } +} + +fn for_in_loop_with_index_expr() { + for i in 0..10 { + println(i) + } +}