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

parser: warn if PostfixExpr followed by ] or ) token (#6214)

This commit is contained in:
Nick Treleaven 2020-08-25 07:50:06 +01:00 committed by GitHub
parent 479bfa28de
commit 818db91a9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View File

@ -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

View File

@ -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 |

View File

@ -0,0 +1,11 @@
fn f(i int) {}
fn test_postfix() {
mut x := 1
_ = (x++)
x--, x-- // OK
f(x++)
a := [x]
_ = a[x--]
}