Skip to content

Commit

Permalink
checker: check expr evaluated but not used
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 7, 2024
1 parent 1571645 commit e0e2218
Show file tree
Hide file tree
Showing 3 changed files with 16 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.error('expression evaluated but not used', node.stmts.last().pos)
}
}
c.stmts(mut node.stmts)
}
}
Expand Down
6 changes: 6 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,6 @@
vlib/v/checker/tests/expr_evaluated_but_not_used.vv:2:3: error: expression evaluated but not used
1 | fn main() {
2 | {1, 2, 3, 4}
| ^
3 | println('works')
4 | }
4 changes: 4 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,4 @@
fn main() {
{1, 2, 3, 4}
println('works')
}

0 comments on commit e0e2218

Please sign in to comment.