From d08a0b5a7ca2077197ad8ea2d02870610f10a42d Mon Sep 17 00:00:00 2001 From: zakuro Date: Tue, 16 Feb 2021 00:41:04 +0900 Subject: [PATCH] parser: check not used expression for all exprs in multi-expr (#8733) --- vlib/v/parser/parser.v | 17 ++++++++++------- .../tests/expr_evaluated_but_not_used_d.out | 9 ++++----- .../tests/expr_evaluated_but_not_used_d.vv | 5 ++--- .../tests/expr_evaluated_but_not_used_e.out | 7 +++++++ .../tests/expr_evaluated_but_not_used_e.vv | 5 +++++ 5 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 vlib/v/parser/tests/expr_evaluated_but_not_used_e.out create mode 100644 vlib/v/parser/tests/expr_evaluated_but_not_used_e.vv diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index b0e5b96c18..853731d965 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1020,13 +1020,16 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() { return p.partial_assign_stmt(left, left_comments) } else if !p.pref.translated - && tok.kind !in [.key_if, .key_match, .key_lock, .key_rlock, .key_select] - && left0 !is ast.CallExpr && (is_top_level || p.tok.kind != .rcbr) - && left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr - && (left0 as ast.InfixExpr).op in [.left_shift, .arrow]) && left0 !is ast.ComptimeCall - && left0 !is ast.SelectorExpr { - p.error_with_pos('expression evaluated but not used', left0.position()) - return ast.Stmt{} + && tok.kind !in [.key_if, .key_match, .key_lock, .key_rlock, .key_select] { + for node in left { + if node !is ast.CallExpr && (is_top_level || p.tok.kind != .rcbr) + && node !is ast.PostfixExpr && !(node is ast.InfixExpr + && (node as ast.InfixExpr).op in [.left_shift, .arrow]) && node !is ast.ComptimeCall + && node !is ast.SelectorExpr { + p.error_with_pos('expression evaluated but not used', node.position()) + return ast.Stmt{} + } + } } pos.update_last_line(p.prev_tok.line_nr) if left.len == 1 { diff --git a/vlib/v/parser/tests/expr_evaluated_but_not_used_d.out b/vlib/v/parser/tests/expr_evaluated_but_not_used_d.out index 663a7c9e4b..03639ea803 100644 --- a/vlib/v/parser/tests/expr_evaluated_but_not_used_d.out +++ b/vlib/v/parser/tests/expr_evaluated_but_not_used_d.out @@ -1,7 +1,6 @@ -vlib/v/parser/tests/expr_evaluated_but_not_used_d.vv:4:5: error: expression evaluated but not used +vlib/v/parser/tests/expr_evaluated_but_not_used_d.vv:4:19: error: expression evaluated but not used 2 | a := 1 3 | b := 2 - 4 | a+b - | ~~~ - 5 | println(a*b) - 6 | } + 4 | println(a*b), a+b + | ~~~ + 5 | } diff --git a/vlib/v/parser/tests/expr_evaluated_but_not_used_d.vv b/vlib/v/parser/tests/expr_evaluated_but_not_used_d.vv index d4cb9e1545..6dbacb764e 100644 --- a/vlib/v/parser/tests/expr_evaluated_but_not_used_d.vv +++ b/vlib/v/parser/tests/expr_evaluated_but_not_used_d.vv @@ -1,6 +1,5 @@ fn main() { a := 1 b := 2 - a+b - println(a*b) -} \ No newline at end of file + println(a*b), a+b +} diff --git a/vlib/v/parser/tests/expr_evaluated_but_not_used_e.out b/vlib/v/parser/tests/expr_evaluated_but_not_used_e.out new file mode 100644 index 0000000000..c31212467c --- /dev/null +++ b/vlib/v/parser/tests/expr_evaluated_but_not_used_e.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/expr_evaluated_but_not_used_e.vv:3:14: error: expression evaluated but not used + 1 | fn main() { + 2 | mut array := [1, 2, 3] + 3 | array << 4, 5 + | ^ + 4 | println(array) + 5 | } diff --git a/vlib/v/parser/tests/expr_evaluated_but_not_used_e.vv b/vlib/v/parser/tests/expr_evaluated_but_not_used_e.vv new file mode 100644 index 0000000000..763624acf3 --- /dev/null +++ b/vlib/v/parser/tests/expr_evaluated_but_not_used_e.vv @@ -0,0 +1,5 @@ +fn main() { + mut array := [1, 2, 3] + array << 4, 5 + println(array) +}