diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 13334cc67e4a31..5160d758455faf 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -517,7 +517,8 @@ fn (mut g Gen) infix_expr_in_op(node ast.InfixExpr) { if node.right is ast.ArrayInit { elem_type := node.right.elem_type elem_sym := g.table.sym(elem_type) - if node.right.exprs.len > 0 { + // TODO: replace ast.Ident check with proper side effect analysis + if node.right.exprs.len > 0 && node.left is ast.Ident { // `a in [1,2,3]` optimization => `a == 1 || a == 2 || a == 3` // avoids an allocation g.write('(') diff --git a/vlib/v/gen/c/testdata/for_in_side_effect.out b/vlib/v/gen/c/testdata/for_in_side_effect.out new file mode 100644 index 00000000000000..e2d8a32577660d --- /dev/null +++ b/vlib/v/gen/c/testdata/for_in_side_effect.out @@ -0,0 +1 @@ +side effect diff --git a/vlib/v/gen/c/testdata/for_in_side_effect.vv b/vlib/v/gen/c/testdata/for_in_side_effect.vv new file mode 100644 index 00000000000000..40eb2237040655 --- /dev/null +++ b/vlib/v/gen/c/testdata/for_in_side_effect.vv @@ -0,0 +1,8 @@ +fn test() bool { + println('side effect') + return true +} + +fn main() { + assert (test() in [false, true]) == true +}