Correctly check for variable use in useless_let_if_seq

This commit is contained in:
mcarton 2016-06-03 19:35:39 +02:00
parent d9c49b205d
commit 5c2a10d703
2 changed files with 19 additions and 5 deletions

View file

@ -69,6 +69,11 @@ impl LateLintPass for LetIfSeq {
let Some(def) = cx.tcx.def_map.borrow().get(&decl.pat.id),
let hir::StmtExpr(ref if_, _) = expr.node,
let hir::ExprIf(ref cond, ref then, ref else_) = if_.node,
{
let mut v = UsedVisitor { cx: cx, id: def.def_id(), used: false };
hir::intravisit::walk_expr(&mut v, cond);
!v.used
},
let Some(value) = check_assign(cx, def.def_id(), then),
], {
let span = codemap::mk_sp(stmt.span.lo, if_.span.hi);
@ -163,13 +168,13 @@ fn check_assign<'e>(cx: &LateContext, decl: hir::def_id::DefId, block: &'e hir::
for s in block.stmts.iter().take(block.stmts.len()-1) {
hir::intravisit::walk_stmt(&mut v, s);
if v.used {
return None;
}
}
return if v.used {
None
} else {
Some(value)
};
return Some(value);
}}
None

View file

@ -6,6 +6,14 @@
fn f() -> bool { true }
fn issue975() -> String {
let mut udn = "dummy".to_string();
if udn.starts_with("uuid:") {
udn = String::from(&udn[5..]);
}
udn
}
fn early_return() -> u8 {
// FIXME: we could extend the lint to include such cases:
let foo;
@ -21,6 +29,7 @@ fn early_return() -> u8 {
fn main() {
early_return();
issue975();
let mut foo = 0;
//~^ ERROR `if _ { .. } else { .. }` is an expression