]> git.lizzy.rs Git - rust.git/commitdiff
Fix needless_bool suggestion with if-else-if-else
authorPhilipp Hansch <dev@phansch.net>
Sun, 4 Aug 2019 20:08:28 +0000 (22:08 +0200)
committerPhilipp Hansch <dev@phansch.net>
Mon, 5 Aug 2019 18:52:38 +0000 (20:52 +0200)
Closes #4334

clippy_lints/src/needless_bool.rs
tests/ui/needless_bool.rs
tests/ui/needless_bool.stderr

index 167f00fee517833a06745bf0384a1554ce09bc61..199420071cc3b0fc273fec9fdc5c795a66944287 100644 (file)
@@ -118,13 +118,15 @@ fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) -> bool
     let parent_id = cx.tcx.hir().get_parent_node(expr.hir_id);
     let parent_node = cx.tcx.hir().get(parent_id);
 
-    if let rustc::hir::Node::Expr(e) = parent_node {
-        if higher::if_block(&e).is_some() {
-            return true;
-        }
+    match parent_node {
+        rustc::hir::Node::Expr(e) => {
+            higher::if_block(&e).is_some()
+        },
+        rustc::hir::Node::Arm(e) => {
+            higher::if_block(&e.body).is_some()
+        },
+        _ => false
     }
-
-    false
 }
 
 declare_lint_pass!(BoolComparison => [BOOL_COMPARISON]);
index 75705525790191c551f88cb7591b992aaa26a3e5..15582f0879512ee16da7d0e10e65f1107b6fe862 100644 (file)
@@ -1,4 +1,5 @@
 #![warn(clippy::needless_bool)]
+#![allow(unused, dead_code, clippy::no_effect)]
 
 use std::cell::Cell;
 
index 30284675f43ffc8d3bc773c71d744612a6d9cd49..3c89e51d4e0fed3550870abdbead1e50c7eabe63 100644 (file)
@@ -1,5 +1,5 @@
 error: this if-then-else expression will always return true
-  --> $DIR/needless_bool.rs:31:5
+  --> $DIR/needless_bool.rs:32:5
    |
 LL | /     if x {
 LL | |         true
@@ -11,7 +11,7 @@ LL | |     };
    = note: `-D clippy::needless-bool` implied by `-D warnings`
 
 error: this if-then-else expression will always return false
-  --> $DIR/needless_bool.rs:36:5
+  --> $DIR/needless_bool.rs:37:5
    |
 LL | /     if x {
 LL | |         false
@@ -21,7 +21,7 @@ LL | |     };
    | |_____^
 
 error: this if-then-else expression returns a bool literal
-  --> $DIR/needless_bool.rs:41:5
+  --> $DIR/needless_bool.rs:42:5
    |
 LL | /     if x {
 LL | |         true
@@ -31,7 +31,7 @@ LL | |     };
    | |_____^ help: you can reduce it to: `x`
 
 error: this if-then-else expression returns a bool literal
-  --> $DIR/needless_bool.rs:46:5
+  --> $DIR/needless_bool.rs:47:5
    |
 LL | /     if x {
 LL | |         false
@@ -41,7 +41,7 @@ LL | |     };
    | |_____^ help: you can reduce it to: `!x`
 
 error: this if-then-else expression returns a bool literal
-  --> $DIR/needless_bool.rs:51:5
+  --> $DIR/needless_bool.rs:52:5
    |
 LL | /     if x && y {
 LL | |         false
@@ -51,7 +51,7 @@ LL | |     };
    | |_____^ help: you can reduce it to: `!(x && y)`
 
 error: this if-then-else expression will always return true
-  --> $DIR/needless_bool.rs:74:5
+  --> $DIR/needless_bool.rs:75:5
    |
 LL | /     if x {
 LL | |         return true;
@@ -61,7 +61,7 @@ LL | |     };
    | |_____^
 
 error: this if-then-else expression will always return false
-  --> $DIR/needless_bool.rs:83:5
+  --> $DIR/needless_bool.rs:84:5
    |
 LL | /     if x {
 LL | |         return false;
@@ -71,7 +71,7 @@ LL | |     };
    | |_____^
 
 error: this if-then-else expression returns a bool literal
-  --> $DIR/needless_bool.rs:92:5
+  --> $DIR/needless_bool.rs:93:5
    |
 LL | /     if x {
 LL | |         return true;
@@ -81,7 +81,7 @@ LL | |     };
    | |_____^ help: you can reduce it to: `return x`
 
 error: this if-then-else expression returns a bool literal
-  --> $DIR/needless_bool.rs:101:5
+  --> $DIR/needless_bool.rs:102:5
    |
 LL | /     if x && y {
 LL | |         return true;
@@ -91,7 +91,7 @@ LL | |     };
    | |_____^ help: you can reduce it to: `return x && y`
 
 error: this if-then-else expression returns a bool literal
-  --> $DIR/needless_bool.rs:110:5
+  --> $DIR/needless_bool.rs:111:5
    |
 LL | /     if x {
 LL | |         return false;
@@ -101,7 +101,7 @@ LL | |     };
    | |_____^ help: you can reduce it to: `return !x`
 
 error: this if-then-else expression returns a bool literal
-  --> $DIR/needless_bool.rs:119:5
+  --> $DIR/needless_bool.rs:120:5
    |
 LL | /     if x && y {
 LL | |         return false;
@@ -111,7 +111,7 @@ LL | |     };
    | |_____^ help: you can reduce it to: `return !(x && y)`
 
 error: equality checks against true are unnecessary
-  --> $DIR/needless_bool.rs:127:8
+  --> $DIR/needless_bool.rs:128:8
    |
 LL |     if x == true {};
    |        ^^^^^^^^^ help: try simplifying it as shown: `x`
@@ -119,25 +119,25 @@ LL |     if x == true {};
    = note: `-D clippy::bool-comparison` implied by `-D warnings`
 
 error: equality checks against false can be replaced by a negation
-  --> $DIR/needless_bool.rs:131:8
+  --> $DIR/needless_bool.rs:132:8
    |
 LL |     if x == false {};
    |        ^^^^^^^^^^ help: try simplifying it as shown: `!x`
 
 error: equality checks against true are unnecessary
-  --> $DIR/needless_bool.rs:141:8
+  --> $DIR/needless_bool.rs:142:8
    |
 LL |     if x == true {};
    |        ^^^^^^^^^ help: try simplifying it as shown: `x`
 
 error: equality checks against false can be replaced by a negation
-  --> $DIR/needless_bool.rs:142:8
+  --> $DIR/needless_bool.rs:143:8
    |
 LL |     if x == false {};
    |        ^^^^^^^^^^ help: try simplifying it as shown: `!x`
 
 error: this if-then-else expression returns a bool literal
-  --> $DIR/needless_bool.rs:151:12
+  --> $DIR/needless_bool.rs:152:12
    |
 LL |       } else if returns_bool() {
    |  ____________^
@@ -145,7 +145,7 @@ LL | |         false
 LL | |     } else {
 LL | |         true
 LL | |     };
-   | |_____^ help: you can reduce it to: `!returns_bool()`
+   | |_____^ help: you can reduce it to: `{ !returns_bool() }`
 
 error: aborting due to 16 previous errors