Add missing unused variable warnings for for loop bindings

This commit is contained in:
Jakub Wieczorek 2014-09-15 22:07:00 +02:00
parent a8d478db51
commit c2a25a4a83
3 changed files with 36 additions and 8 deletions

View File

@ -131,8 +131,8 @@ enum LoopKind<'a> {
LoopLoop,
/// A `while` loop, with the given expression as condition.
WhileLoop(&'a Expr),
/// A `for` loop.
ForLoop,
/// A `for` loop, with the given pattern to bind.
ForLoop(&'a Pat),
}
#[deriving(PartialEq)]
@ -1024,8 +1024,8 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode)
self.propagate_through_loop(expr, WhileLoop(&**cond), &**blk, succ)
}
ExprForLoop(_, ref head, ref blk, _) => {
let ln = self.propagate_through_loop(expr, ForLoop, &**blk, succ);
ExprForLoop(ref pat, ref head, ref blk, _) => {
let ln = self.propagate_through_loop(expr, ForLoop(&**pat), &**blk, succ);
self.propagate_through_expr(&**head, ln)
}
@ -1355,7 +1355,8 @@ fn propagate_through_loop(&mut self,
expr.id, block_to_string(body));
let cond_ln = match kind {
LoopLoop | ForLoop => ln,
LoopLoop => ln,
ForLoop(ref pat) => self.define_bindings_in_pat(*pat, ln),
WhileLoop(ref cond) => self.propagate_through_expr(&**cond, ln),
};
let body_ln = self.with_loop_nodes(expr.id, succ, ln, |this| {
@ -1367,7 +1368,10 @@ fn propagate_through_loop(&mut self,
first_merge = false;
let new_cond_ln = match kind {
LoopLoop | ForLoop => ln,
LoopLoop => ln,
ForLoop(ref pat) => {
self.define_bindings_in_pat(*pat, ln)
}
WhileLoop(ref cond) => {
self.propagate_through_expr(&**cond, ln)
}
@ -1453,6 +1457,12 @@ fn check_expr(this: &mut Liveness, expr: &Expr) {
visit::walk_expr(this, expr);
}
ExprForLoop(ref pat, _, _, _) => {
this.pat_bindings(&**pat, |this, ln, var, sp, id| {
this.warn_about_unused(sp, id, ln, var);
});
}
// no correctness conditions related to liveness
ExprCall(..) | ExprMethodCall(..) | ExprIf(..) | ExprMatch(..) |
ExprWhile(..) | ExprLoop(..) | ExprIndex(..) | ExprField(..) |
@ -1461,7 +1471,7 @@ fn check_expr(this: &mut Liveness, expr: &Expr) {
ExprAgain(..) | ExprLit(_) | ExprBlock(..) |
ExprMac(..) | ExprAddrOf(..) | ExprStruct(..) | ExprRepeat(..) |
ExprParen(..) | ExprFnBlock(..) | ExprProc(..) | ExprUnboxedFn(..) |
ExprPath(..) | ExprBox(..) | ExprForLoop(..) => {
ExprPath(..) | ExprBox(..) => {
visit::walk_expr(this, expr);
}
}

View File

@ -557,7 +557,7 @@ fn get_branches<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
let tcx = bcx.tcx();
let mut found: Vec<Opt> = vec![];
for (i, br) in m.iter().enumerate() {
for br in m.iter() {
let cur = *br.pats.get(col);
let opt = match cur.node {
ast::PatLit(ref l) => ConstantValue(ConstantExpr(&**l)),

View File

@ -82,5 +82,23 @@ fn f4b() -> int {
}
}
fn f5a() {
for x in range(1i, 10) { }
//~^ ERROR unused variable: `x`
}
fn f5b() {
for (x, _) in [1i, 2, 3].iter().enumerate() { }
//~^ ERROR unused variable: `x`
}
fn f5c() {
for (_, x) in [1i, 2, 3].iter().enumerate() {
//~^ ERROR unused variable: `x`
continue;
std::os::set_exit_status(*x); //~ WARNING unreachable statement
}
}
fn main() {
}