From 258f8f6af960c6d369d9991ce2fc56f87664bfa4 Mon Sep 17 00:00:00 2001 From: Takahiro Yaota Date: Thu, 26 Nov 2020 08:28:41 +0900 Subject: [PATCH] parser: report unused expression error in if and or blocks (#6953) --- vlib/regex/regex.v | 4 ++-- vlib/v/parser/parser.v | 8 ++++---- vlib/v/parser/tests/expr_evaluated_but_not_used_if.out | 7 +++++++ vlib/v/parser/tests/expr_evaluated_but_not_used_if.vv | 8 ++++++++ vlib/v/parser/tests/expr_evaluated_but_not_used_or.out | 7 +++++++ vlib/v/parser/tests/expr_evaluated_but_not_used_or.vv | 10 ++++++++++ vlib/x/json2/decoder.v | 4 ++-- 7 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 vlib/v/parser/tests/expr_evaluated_but_not_used_if.out create mode 100644 vlib/v/parser/tests/expr_evaluated_but_not_used_if.vv create mode 100644 vlib/v/parser/tests/expr_evaluated_but_not_used_or.out create mode 100644 vlib/v/parser/tests/expr_evaluated_but_not_used_or.vv diff --git a/vlib/regex/regex.v b/vlib/regex/regex.v index 5f62d9438f..b3fc542676 100644 --- a/vlib/regex/regex.v +++ b/vlib/regex/regex.v @@ -1650,7 +1650,7 @@ pub fn (mut re RE) match_base(in_txt byteptr, in_txt_len int ) (int,int) { // manage ist_dot_char - m_state == .end + m_state = .end break //return no_match_found,0 } @@ -1822,7 +1822,7 @@ pub fn (mut re RE) match_base(in_txt byteptr, in_txt_len int ) (int,int) { //println("ist_or_branch False pc: $pc") } re.prog[pc].reset() - m_state == .ist_load + m_state = .ist_load continue } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index f0361374ec..1b7c051f83 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -886,10 +886,10 @@ 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 is_top_level && tok.kind !in - [.key_if, .key_match, .key_lock, .key_rlock, .key_select] && left0 !is ast.CallExpr && - left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr && - (left0 as ast.InfixExpr).op in [.left_shift, .arrow]) && left0 !is ast.ComptimeCall { + } else if 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 { p.error_with_pos('expression evaluated but not used', left0.position()) } if left.len == 1 { diff --git a/vlib/v/parser/tests/expr_evaluated_but_not_used_if.out b/vlib/v/parser/tests/expr_evaluated_but_not_used_if.out new file mode 100644 index 0000000000..f35579a9f9 --- /dev/null +++ b/vlib/v/parser/tests/expr_evaluated_but_not_used_if.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/expr_evaluated_but_not_used_if.vv:3:5: error: expression evaluated but not used + 1 | fn main() { + 2 | if true { + 3 | 1 + 1 + | ~~~~~ + 4 | 1 + 1 + 5 | } else { diff --git a/vlib/v/parser/tests/expr_evaluated_but_not_used_if.vv b/vlib/v/parser/tests/expr_evaluated_but_not_used_if.vv new file mode 100644 index 0000000000..8cd5a96030 --- /dev/null +++ b/vlib/v/parser/tests/expr_evaluated_but_not_used_if.vv @@ -0,0 +1,8 @@ +fn main() { + if true { + 1 + 1 + 1 + 1 + } else { + 1 + 1 + } +} diff --git a/vlib/v/parser/tests/expr_evaluated_but_not_used_or.out b/vlib/v/parser/tests/expr_evaluated_but_not_used_or.out new file mode 100644 index 0000000000..70b3e7e7d3 --- /dev/null +++ b/vlib/v/parser/tests/expr_evaluated_but_not_used_or.out @@ -0,0 +1,7 @@ +vlib/v/parser/tests/expr_evaluated_but_not_used_or.vv:3:5: error: expression evaluated but not used + 1 | fn main() { + 2 | f() or { + 3 | 0 + | ^ + 4 | 1 + 5 | } diff --git a/vlib/v/parser/tests/expr_evaluated_but_not_used_or.vv b/vlib/v/parser/tests/expr_evaluated_but_not_used_or.vv new file mode 100644 index 0000000000..4a3a82ae80 --- /dev/null +++ b/vlib/v/parser/tests/expr_evaluated_but_not_used_or.vv @@ -0,0 +1,10 @@ +fn main() { + f() or { + 0 + 1 + } +} + +fn f() ?int { + return none +} diff --git a/vlib/x/json2/decoder.v b/vlib/x/json2/decoder.v index 69be6b2f7c..938ba9eef1 100644 --- a/vlib/x/json2/decoder.v +++ b/vlib/x/json2/decoder.v @@ -128,7 +128,7 @@ fn (p Parser) is_singlequote() bool { fn (mut p Parser) detect_parse_mode() { src := p.scanner.text if src.len > 1 && src[0].is_digit() && !src[1].is_digit() { - p.mode == .invalid + p.mode = .invalid return } @@ -136,7 +136,7 @@ fn (mut p Parser) detect_parse_mode() { p.n_tok = p.scanner.scan() if src.len == 1 && p.tok.kind == .string && p.n_tok.kind == .eof { - p.mode == .invalid + p.mode = .invalid return }