Skip to content

Commit

Permalink
checker: check expr evaluated but not used (fix #21436) (#21816)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Jul 8, 2024
1 parent f6de36d commit 14d378c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,12 @@ fn (mut c Checker) block(mut node ast.Block) {
c.stmts(mut node.stmts)
c.inside_unsafe = prev_unsafe
} else {
if node.stmts.len > 0 && node.stmts.last() is ast.ExprStmt {
last_stmt := node.stmts.last() as ast.ExprStmt
if last_stmt.expr !in [ast.CallExpr, ast.IfExpr, ast.MatchExpr, ast.InfixExpr] {
c.warn('expression evaluated but not used', node.stmts.last().pos)
}
}
c.stmts(mut node.stmts)
}
}
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/expr_evaluated_but_not_used.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/expr_evaluated_but_not_used.vv:7:3: warning: expression evaluated but not used
5 | println(3)
6 | println(4)
7 | 5
| ^
8 | }
9 | println('works')
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/expr_evaluated_but_not_used.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() {
{
println(1)
println(2)
println(3)
println(4)
5
}
println('works')
}

0 comments on commit 14d378c

Please sign in to comment.