Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing unused variable warnings for for loop bindings #17279

Merged
merged 1 commit into from Sep 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -1024,8 +1024,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
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)
}

Expand Down Expand Up @@ -1355,7 +1355,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
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| {
Expand All @@ -1367,7 +1368,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
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)
}
Expand Down Expand Up @@ -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(..) |
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
18 changes: 18 additions & 0 deletions src/test/compile-fail/liveness-unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}