Skip to content

Commit c2a25a4

Browse files
author
Jakub Wieczorek
committed
Add missing unused variable warnings for for loop bindings
1 parent a8d478d commit c2a25a4

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/librustc/middle/liveness.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ enum LoopKind<'a> {
131131
LoopLoop,
132132
/// A `while` loop, with the given expression as condition.
133133
WhileLoop(&'a Expr),
134-
/// A `for` loop.
135-
ForLoop,
134+
/// A `for` loop, with the given pattern to bind.
135+
ForLoop(&'a Pat),
136136
}
137137

138138
#[deriving(PartialEq)]
@@ -1024,8 +1024,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10241024
self.propagate_through_loop(expr, WhileLoop(&**cond), &**blk, succ)
10251025
}
10261026

1027-
ExprForLoop(_, ref head, ref blk, _) => {
1028-
let ln = self.propagate_through_loop(expr, ForLoop, &**blk, succ);
1027+
ExprForLoop(ref pat, ref head, ref blk, _) => {
1028+
let ln = self.propagate_through_loop(expr, ForLoop(&**pat), &**blk, succ);
10291029
self.propagate_through_expr(&**head, ln)
10301030
}
10311031

@@ -1355,7 +1355,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
13551355
expr.id, block_to_string(body));
13561356

13571357
let cond_ln = match kind {
1358-
LoopLoop | ForLoop => ln,
1358+
LoopLoop => ln,
1359+
ForLoop(ref pat) => self.define_bindings_in_pat(*pat, ln),
13591360
WhileLoop(ref cond) => self.propagate_through_expr(&**cond, ln),
13601361
};
13611362
let body_ln = self.with_loop_nodes(expr.id, succ, ln, |this| {
@@ -1367,7 +1368,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
13671368
first_merge = false;
13681369

13691370
let new_cond_ln = match kind {
1370-
LoopLoop | ForLoop => ln,
1371+
LoopLoop => ln,
1372+
ForLoop(ref pat) => {
1373+
self.define_bindings_in_pat(*pat, ln)
1374+
}
13711375
WhileLoop(ref cond) => {
13721376
self.propagate_through_expr(&**cond, ln)
13731377
}
@@ -1453,6 +1457,12 @@ fn check_expr(this: &mut Liveness, expr: &Expr) {
14531457
visit::walk_expr(this, expr);
14541458
}
14551459

1460+
ExprForLoop(ref pat, _, _, _) => {
1461+
this.pat_bindings(&**pat, |this, ln, var, sp, id| {
1462+
this.warn_about_unused(sp, id, ln, var);
1463+
});
1464+
}
1465+
14561466
// no correctness conditions related to liveness
14571467
ExprCall(..) | ExprMethodCall(..) | ExprIf(..) | ExprMatch(..) |
14581468
ExprWhile(..) | ExprLoop(..) | ExprIndex(..) | ExprField(..) |
@@ -1461,7 +1471,7 @@ fn check_expr(this: &mut Liveness, expr: &Expr) {
14611471
ExprAgain(..) | ExprLit(_) | ExprBlock(..) |
14621472
ExprMac(..) | ExprAddrOf(..) | ExprStruct(..) | ExprRepeat(..) |
14631473
ExprParen(..) | ExprFnBlock(..) | ExprProc(..) | ExprUnboxedFn(..) |
1464-
ExprPath(..) | ExprBox(..) | ExprForLoop(..) => {
1474+
ExprPath(..) | ExprBox(..) => {
14651475
visit::walk_expr(this, expr);
14661476
}
14671477
}

src/librustc/middle/trans/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ fn get_branches<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
557557
let tcx = bcx.tcx();
558558

559559
let mut found: Vec<Opt> = vec![];
560-
for (i, br) in m.iter().enumerate() {
560+
for br in m.iter() {
561561
let cur = *br.pats.get(col);
562562
let opt = match cur.node {
563563
ast::PatLit(ref l) => ConstantValue(ConstantExpr(&**l)),

src/test/compile-fail/liveness-unused.rs

+18
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,23 @@ fn f4b() -> int {
8282
}
8383
}
8484

85+
fn f5a() {
86+
for x in range(1i, 10) { }
87+
//~^ ERROR unused variable: `x`
88+
}
89+
90+
fn f5b() {
91+
for (x, _) in [1i, 2, 3].iter().enumerate() { }
92+
//~^ ERROR unused variable: `x`
93+
}
94+
95+
fn f5c() {
96+
for (_, x) in [1i, 2, 3].iter().enumerate() {
97+
//~^ ERROR unused variable: `x`
98+
continue;
99+
std::os::set_exit_status(*x); //~ WARNING unreachable statement
100+
}
101+
}
102+
85103
fn main() {
86104
}

0 commit comments

Comments
 (0)