From 5c2a10d703ea65741375ba6c27b64d9596b380fc Mon Sep 17 00:00:00 2001 From: mcarton Date: Fri, 3 Jun 2016 19:35:39 +0200 Subject: [PATCH] Correctly check for variable use in `useless_let_if_seq` --- clippy_lints/src/let_if_seq.rs | 15 ++++++++++----- tests/compile-fail/let_if_seq.rs | 9 +++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/let_if_seq.rs b/clippy_lints/src/let_if_seq.rs index 1eb2f756cbf..09172014c8c 100644 --- a/clippy_lints/src/let_if_seq.rs +++ b/clippy_lints/src/let_if_seq.rs @@ -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 diff --git a/tests/compile-fail/let_if_seq.rs b/tests/compile-fail/let_if_seq.rs index 011848e95dd..caa8bae22fd 100644 --- a/tests/compile-fail/let_if_seq.rs +++ b/tests/compile-fail/let_if_seq.rs @@ -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