parser: warn if PostfixExpr followed by `]` or `)` token (#6214)
parent
479bfa28de
commit
818db91a9e
|
@ -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
|
||||
|
|
|
@ -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 |
|
|
@ -0,0 +1,11 @@
|
|||
fn f(i int) {}
|
||||
|
||||
fn test_postfix() {
|
||||
mut x := 1
|
||||
_ = (x++)
|
||||
x--, x-- // OK
|
||||
f(x++)
|
||||
a := [x]
|
||||
_ = a[x--]
|
||||
}
|
||||
|
Loading…
Reference in New Issue