Merge pull request #979 from Manishearth/fx-975

Correctly check for variable use in `useless_let_if_seq`
This commit is contained in:
llogiq 2016-06-03 21:42:41 +02:00
commit 7944fa811b
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 Some(def) = cx.tcx.def_map.borrow().get(&decl.pat.id),
let hir::StmtExpr(ref if_, _) = expr.node, let hir::StmtExpr(ref if_, _) = expr.node,
let hir::ExprIf(ref cond, ref then, ref else_) = if_.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 Some(value) = check_assign(cx, def.def_id(), then),
], { ], {
let span = codemap::mk_sp(stmt.span.lo, if_.span.hi); 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) { for s in block.stmts.iter().take(block.stmts.len()-1) {
hir::intravisit::walk_stmt(&mut v, s); hir::intravisit::walk_stmt(&mut v, s);
if v.used {
return None;
}
} }
return if v.used { return Some(value);
None
} else {
Some(value)
};
}} }}
None None

View file

@ -6,6 +6,14 @@
fn f() -> bool { true } 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 { fn early_return() -> u8 {
// FIXME: we could extend the lint to include such cases: // FIXME: we could extend the lint to include such cases:
let foo; let foo;
@ -21,6 +29,7 @@ fn early_return() -> u8 {
fn main() { fn main() {
early_return(); early_return();
issue975();
let mut foo = 0; let mut foo = 0;
//~^ ERROR `if _ { .. } else { .. }` is an expression //~^ ERROR `if _ { .. } else { .. }` is an expression