]> git.lizzy.rs Git - rust.git/commitdiff
Fix false-positive in `USELESS_LET_IF_SEQ`
authormcarton <cartonmartin+git@gmail.com>
Sun, 5 Jun 2016 19:38:15 +0000 (21:38 +0200)
committermcarton <cartonmartin+git@gmail.com>
Sun, 5 Jun 2016 19:46:48 +0000 (21:46 +0200)
clippy_lints/src/let_if_seq.rs
tests/compile-fail/let_if_seq.rs

index 09172014c8cbf66facb2b248c53f600d858f6e60..a85cb52f2dc6e0d4687750474fb0ca879710c2e9 100644 (file)
@@ -69,12 +69,9 @@ fn check_block(&mut self, cx: &LateContext, block: &hir::Block) {
                 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
-                },
+                !used_in_expr(cx, def.def_id(), cond),
                 let Some(value) = check_assign(cx, def.def_id(), then),
+                !used_in_expr(cx, def.def_id(), value),
             ], {
                 let span = codemap::mk_sp(stmt.span.lo, if_.span.hi);
 
@@ -179,3 +176,13 @@ fn check_assign<'e>(cx: &LateContext, decl: hir::def_id::DefId, block: &'e hir::
 
     None
 }
+
+fn used_in_expr(cx: &LateContext, id: hir::def_id::DefId, expr: &hir::Expr) -> bool {
+    let mut v = UsedVisitor {
+        cx: cx,
+        id: id,
+        used: false
+    };
+    hir::intravisit::walk_expr(&mut v, expr);
+    v.used
+}
index caa8bae22fd857cb9fbc385f07db0e037c20040d..0b086faf077d45ed565f96baa8a616e87f3b5116 100644 (file)
@@ -5,6 +5,27 @@
 #![deny(useless_let_if_seq)]
 
 fn f() -> bool { true }
+fn g(x: i32) -> i32 { x + 1 }
+
+fn issue985() -> i32 {
+    let mut x = 42;
+    if f() {
+        x = g(x);
+    }
+
+    x
+}
+
+fn issue985_alt() -> i32 {
+    let mut x = 42;
+    if f() {
+        f();
+    } else {
+        x = g(x);
+    }
+
+    x
+}
 
 fn issue975() -> String {
     let mut udn = "dummy".to_string();
@@ -30,6 +51,8 @@ fn early_return() -> u8 {
 fn main() {
     early_return();
     issue975();
+    issue985();
+    issue985_alt();
 
     let mut foo = 0;
     //~^ ERROR `if _ { .. } else { .. }` is an expression