diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index dfc34118fe..b5be2a9595 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -261,6 +261,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { } } else if p.tok.kind in [.inc, .dec] { // Postfix + // detect `f(x++)`, `a[x++]` + if p.peek_tok.kind in [.rpar, .rsbr] && + p.mod !in ['builtin', 'regex', 'strconv'] { // temp + p.warn_with_pos('`$p.tok.kind` operator can only be used as a statement', p.peek_tok.position()) + } node = ast.PostfixExpr{ op: p.tok.kind expr: node diff --git a/vlib/v/parser/tests/postfix_err.out b/vlib/v/parser/tests/postfix_err.out new file mode 100644 index 0000000000..37f41a99dd --- /dev/null +++ b/vlib/v/parser/tests/postfix_err.out @@ -0,0 +1,21 @@ +vlib/v/parser/tests/postfix_err.v:5:10: warning: `++` operator can only be used as a statement + 3 | fn test_postfix() { + 4 | mut x := 1 + 5 | _ = (x++) + | ^ + 6 | x--, x-- // OK + 7 | f(x++) +vlib/v/parser/tests/postfix_err.v:7:7: warning: `++` operator can only be used as a statement + 5 | _ = (x++) + 6 | x--, x-- // OK + 7 | f(x++) + | ^ + 8 | a := [x] + 9 | _ = a[x--] +vlib/v/parser/tests/postfix_err.v:9:11: warning: `--` operator can only be used as a statement + 7 | f(x++) + 8 | a := [x] + 9 | _ = a[x--] + | ^ + 10 | } + 11 | diff --git a/vlib/v/parser/tests/postfix_err.vv b/vlib/v/parser/tests/postfix_err.vv new file mode 100644 index 0000000000..a1ba568893 --- /dev/null +++ b/vlib/v/parser/tests/postfix_err.vv @@ -0,0 +1,11 @@ +fn f(i int) {} + +fn test_postfix() { + mut x := 1 + _ = (x++) + x--, x-- // OK + f(x++) + a := [x] + _ = a[x--] +} +