parser: report unused expression error in if and or blocks (#6953)

pull/6954/head
Takahiro Yaota 2020-11-26 08:28:41 +09:00 committed by GitHub
parent 2957541e48
commit 258f8f6af9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 8 deletions

View File

@ -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
}

View File

@ -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 {

View 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 {

View File

@ -0,0 +1,8 @@
fn main() {
if true {
1 + 1
1 + 1
} else {
1 + 1
}
}

View 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 | }

View File

@ -0,0 +1,10 @@
fn main() {
f() or {
0
1
}
}
fn f() ?int {
return none
}

View File

@ -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
}