mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: report unused expression error in if and or blocks (#6953)
This commit is contained in:
@@ -1650,7 +1650,7 @@ pub fn (mut re RE) match_base(in_txt byteptr, in_txt_len int ) (int,int) {
|
|||||||
|
|
||||||
// manage ist_dot_char
|
// manage ist_dot_char
|
||||||
|
|
||||||
m_state == .end
|
m_state = .end
|
||||||
break
|
break
|
||||||
//return no_match_found,0
|
//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")
|
//println("ist_or_branch False pc: $pc")
|
||||||
}
|
}
|
||||||
re.prog[pc].reset()
|
re.prog[pc].reset()
|
||||||
m_state == .ist_load
|
m_state = .ist_load
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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() {
|
if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() {
|
||||||
return p.partial_assign_stmt(left, left_comments)
|
return p.partial_assign_stmt(left, left_comments)
|
||||||
} else if is_top_level && tok.kind !in
|
} else if tok.kind !in [.key_if, .key_match, .key_lock, .key_rlock, .key_select] &&
|
||||||
[.key_if, .key_match, .key_lock, .key_rlock, .key_select] && left0 !is ast.CallExpr &&
|
left0 !is ast.CallExpr && (is_top_level || p.tok.kind != .rcbr) && left0 !is ast.PostfixExpr &&
|
||||||
left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr &&
|
!(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op in [.left_shift, .arrow]) && left0 !is
|
||||||
(left0 as ast.InfixExpr).op in [.left_shift, .arrow]) && left0 !is ast.ComptimeCall {
|
ast.ComptimeCall {
|
||||||
p.error_with_pos('expression evaluated but not used', left0.position())
|
p.error_with_pos('expression evaluated but not used', left0.position())
|
||||||
}
|
}
|
||||||
if left.len == 1 {
|
if left.len == 1 {
|
||||||
|
|||||||
7
vlib/v/parser/tests/expr_evaluated_but_not_used_if.out
Normal file
7
vlib/v/parser/tests/expr_evaluated_but_not_used_if.out
Normal file
@@ -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 {
|
||||||
8
vlib/v/parser/tests/expr_evaluated_but_not_used_if.vv
Normal file
8
vlib/v/parser/tests/expr_evaluated_but_not_used_if.vv
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fn main() {
|
||||||
|
if true {
|
||||||
|
1 + 1
|
||||||
|
1 + 1
|
||||||
|
} else {
|
||||||
|
1 + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
7
vlib/v/parser/tests/expr_evaluated_but_not_used_or.out
Normal file
7
vlib/v/parser/tests/expr_evaluated_but_not_used_or.out
Normal file
@@ -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 | }
|
||||||
10
vlib/v/parser/tests/expr_evaluated_but_not_used_or.vv
Normal file
10
vlib/v/parser/tests/expr_evaluated_but_not_used_or.vv
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
fn main() {
|
||||||
|
f() or {
|
||||||
|
0
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f() ?int {
|
||||||
|
return none
|
||||||
|
}
|
||||||
@@ -128,7 +128,7 @@ fn (p Parser) is_singlequote() bool {
|
|||||||
fn (mut p Parser) detect_parse_mode() {
|
fn (mut p Parser) detect_parse_mode() {
|
||||||
src := p.scanner.text
|
src := p.scanner.text
|
||||||
if src.len > 1 && src[0].is_digit() && !src[1].is_digit() {
|
if src.len > 1 && src[0].is_digit() && !src[1].is_digit() {
|
||||||
p.mode == .invalid
|
p.mode = .invalid
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +136,7 @@ fn (mut p Parser) detect_parse_mode() {
|
|||||||
p.n_tok = p.scanner.scan()
|
p.n_tok = p.scanner.scan()
|
||||||
|
|
||||||
if src.len == 1 && p.tok.kind == .string && p.n_tok.kind == .eof {
|
if src.len == 1 && p.tok.kind == .string && p.n_tok.kind == .eof {
|
||||||
p.mode == .invalid
|
p.mode = .invalid
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user