diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8fce5a8aae..a9b9377e52 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -628,6 +628,9 @@ fn (mut c Checker) fail_if_immutable(expr_ ast.Expr) (string, token.Pos) { ast.PrefixExpr { to_lock, pos = c.fail_if_immutable(expr.right) } + ast.PostfixExpr { + to_lock, pos = c.fail_if_immutable(expr.expr) + } ast.SelectorExpr { if expr.expr_type == 0 { return '', pos diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 63ce4e7dbc..9fc7940c9f 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -457,7 +457,7 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden if p.peek_tok.kind in [.rpar, .rsbr] { if !p.inside_ct_if_expr { p.warn_with_pos('`$p.tok.kind` operator can only be used as a statement', - p.peek_tok.pos()) + p.tok.pos()) } } if p.tok.kind in [.inc, .dec] && p.prev_tok.line_nr != p.tok.line_nr { diff --git a/vlib/v/parser/tests/postfix_err.out b/vlib/v/parser/tests/postfix_err.out deleted file mode 100644 index d83ccfab12..0000000000 --- a/vlib/v/parser/tests/postfix_err.out +++ /dev/null @@ -1,21 +0,0 @@ -vlib/v/parser/tests/postfix_err.vv: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.vv: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.vv: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_a.out b/vlib/v/parser/tests/postfix_err_a.out new file mode 100644 index 0000000000..f1130455cb --- /dev/null +++ b/vlib/v/parser/tests/postfix_err_a.out @@ -0,0 +1,21 @@ +vlib/v/parser/tests/postfix_err_a.vv:5:8: 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_a.vv:7:5: 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_a.vv:9:9: 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_a.vv similarity index 100% rename from vlib/v/parser/tests/postfix_err.vv rename to vlib/v/parser/tests/postfix_err_a.vv diff --git a/vlib/v/parser/tests/postfix_err_b.out b/vlib/v/parser/tests/postfix_err_b.out new file mode 100644 index 0000000000..ea6fb0c962 --- /dev/null +++ b/vlib/v/parser/tests/postfix_err_b.out @@ -0,0 +1,14 @@ +vlib/v/parser/tests/postfix_err_b.vv:7:14: warning: `++` operator can only be used as a statement + 5 | for _ in 0..3 { + 6 | unsafe { + 7 | *(arrayptr++) = 0 + | ~~ + 8 | } + 9 | } +vlib/v/parser/tests/postfix_err_b.vv:3:18: warning: cannot cast a fixed array (use e.g. `&arr[0]` instead) + 1 | fn main(){ + 2 | mut array := [3]int{} + 3 | mut arrayptr := &int(array) + | ~~~~~~~~~~~ + 4 | + 5 | for _ in 0..3 { diff --git a/vlib/v/parser/tests/postfix_err_b.vv b/vlib/v/parser/tests/postfix_err_b.vv new file mode 100644 index 0000000000..1aff3af502 --- /dev/null +++ b/vlib/v/parser/tests/postfix_err_b.vv @@ -0,0 +1,10 @@ +fn main(){ + mut array := [3]int{} + mut arrayptr := &int(array) + + for _ in 0..3 { + unsafe { + *(arrayptr++) = 0 + } + } +}