1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: check not used expression for all exprs in multi-expr (#8733)

This commit is contained in:
zakuro 2021-02-16 00:41:04 +09:00 committed by GitHub
parent fe007f9b16
commit d08a0b5a7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 15 deletions

View File

@ -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() { 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 !p.pref.translated } else if !p.pref.translated
&& tok.kind !in [.key_if, .key_match, .key_lock, .key_rlock, .key_select] && tok.kind !in [.key_if, .key_match, .key_lock, .key_rlock, .key_select] {
&& left0 !is ast.CallExpr && (is_top_level || p.tok.kind != .rcbr) for node in left {
&& left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr if node !is ast.CallExpr && (is_top_level || p.tok.kind != .rcbr)
&& (left0 as ast.InfixExpr).op in [.left_shift, .arrow]) && left0 !is ast.ComptimeCall && node !is ast.PostfixExpr && !(node is ast.InfixExpr
&& left0 !is ast.SelectorExpr { && (node as ast.InfixExpr).op in [.left_shift, .arrow]) && node !is ast.ComptimeCall
p.error_with_pos('expression evaluated but not used', left0.position()) && node !is ast.SelectorExpr {
return ast.Stmt{} p.error_with_pos('expression evaluated but not used', node.position())
return ast.Stmt{}
}
}
} }
pos.update_last_line(p.prev_tok.line_nr) pos.update_last_line(p.prev_tok.line_nr)
if left.len == 1 { if left.len == 1 {

View File

@ -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 2 | a := 1
3 | b := 2 3 | b := 2
4 | a+b 4 | println(a*b), a+b
| ~~~ | ~~~
5 | println(a*b) 5 | }
6 | }

View File

@ -1,6 +1,5 @@
fn main() { fn main() {
a := 1 a := 1
b := 2 b := 2
a+b println(a*b), a+b
println(a*b) }
}

View File

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

View File

@ -0,0 +1,5 @@
fn main() {
mut array := [1, 2, 3]
array << 4, 5
println(array)
}