Fix needless_bool suggestion with if-else-if-else

Closes #4334
This commit is contained in:
Philipp Hansch 2019-08-04 22:08:28 +02:00
parent a813c057d5
commit 4cfb0966a1
No known key found for this signature in database
GPG key ID: 82AA61CAA11397E6
3 changed files with 26 additions and 23 deletions

View 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_id = cx.tcx.hir().get_parent_node(expr.hir_id);
let parent_node = cx.tcx.hir().get(parent_id); let parent_node = cx.tcx.hir().get(parent_id);
if let rustc::hir::Node::Expr(e) = parent_node { match parent_node {
if higher::if_block(&e).is_some() { rustc::hir::Node::Expr(e) => {
return true; 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]); declare_lint_pass!(BoolComparison => [BOOL_COMPARISON]);

View file

@ -1,4 +1,5 @@
#![warn(clippy::needless_bool)] #![warn(clippy::needless_bool)]
#![allow(unused, dead_code, clippy::no_effect)]
use std::cell::Cell; use std::cell::Cell;

View file

@ -1,5 +1,5 @@
error: this if-then-else expression will always return true 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 | / if x {
LL | | true LL | | true
@ -11,7 +11,7 @@ LL | | };
= note: `-D clippy::needless-bool` implied by `-D warnings` = note: `-D clippy::needless-bool` implied by `-D warnings`
error: this if-then-else expression will always return false 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 | / if x {
LL | | false LL | | false
@ -21,7 +21,7 @@ LL | | };
| |_____^ | |_____^
error: this if-then-else expression returns a bool literal 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 | / if x {
LL | | true LL | | true
@ -31,7 +31,7 @@ LL | | };
| |_____^ help: you can reduce it to: `x` | |_____^ help: you can reduce it to: `x`
error: this if-then-else expression returns a bool literal 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 | / if x {
LL | | false LL | | false
@ -41,7 +41,7 @@ LL | | };
| |_____^ help: you can reduce it to: `!x` | |_____^ help: you can reduce it to: `!x`
error: this if-then-else expression returns a bool literal 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 | / if x && y {
LL | | false LL | | false
@ -51,7 +51,7 @@ LL | | };
| |_____^ help: you can reduce it to: `!(x && y)` | |_____^ help: you can reduce it to: `!(x && y)`
error: this if-then-else expression will always return true 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 | / if x {
LL | | return true; LL | | return true;
@ -61,7 +61,7 @@ LL | | };
| |_____^ | |_____^
error: this if-then-else expression will always return false 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 | / if x {
LL | | return false; LL | | return false;
@ -71,7 +71,7 @@ LL | | };
| |_____^ | |_____^
error: this if-then-else expression returns a bool literal 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 | / if x {
LL | | return true; LL | | return true;
@ -81,7 +81,7 @@ LL | | };
| |_____^ help: you can reduce it to: `return x` | |_____^ help: you can reduce it to: `return x`
error: this if-then-else expression returns a bool literal 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 | / if x && y {
LL | | return true; LL | | return true;
@ -91,7 +91,7 @@ LL | | };
| |_____^ help: you can reduce it to: `return x && y` | |_____^ help: you can reduce it to: `return x && y`
error: this if-then-else expression returns a bool literal 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 | / if x {
LL | | return false; LL | | return false;
@ -101,7 +101,7 @@ LL | | };
| |_____^ help: you can reduce it to: `return !x` | |_____^ help: you can reduce it to: `return !x`
error: this if-then-else expression returns a bool literal 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 | / if x && y {
LL | | return false; LL | | return false;
@ -111,7 +111,7 @@ LL | | };
| |_____^ help: you can reduce it to: `return !(x && y)` | |_____^ help: you can reduce it to: `return !(x && y)`
error: equality checks against true are unnecessary error: equality checks against true are unnecessary
--> $DIR/needless_bool.rs:127:8 --> $DIR/needless_bool.rs:128:8
| |
LL | if x == true {}; LL | if x == true {};
| ^^^^^^^^^ help: try simplifying it as shown: `x` | ^^^^^^^^^ help: try simplifying it as shown: `x`
@ -119,25 +119,25 @@ LL | if x == true {};
= note: `-D clippy::bool-comparison` implied by `-D warnings` = note: `-D clippy::bool-comparison` implied by `-D warnings`
error: equality checks against false can be replaced by a negation 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 {}; LL | if x == false {};
| ^^^^^^^^^^ help: try simplifying it as shown: `!x` | ^^^^^^^^^^ help: try simplifying it as shown: `!x`
error: equality checks against true are unnecessary error: equality checks against true are unnecessary
--> $DIR/needless_bool.rs:141:8 --> $DIR/needless_bool.rs:142:8
| |
LL | if x == true {}; LL | if x == true {};
| ^^^^^^^^^ help: try simplifying it as shown: `x` | ^^^^^^^^^ help: try simplifying it as shown: `x`
error: equality checks against false can be replaced by a negation 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 {}; LL | if x == false {};
| ^^^^^^^^^^ help: try simplifying it as shown: `!x` | ^^^^^^^^^^ help: try simplifying it as shown: `!x`
error: this if-then-else expression returns a bool literal 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() { LL | } else if returns_bool() {
| ____________^ | ____________^
@ -145,7 +145,7 @@ LL | | false
LL | | } else { LL | | } else {
LL | | true LL | | true
LL | | }; LL | | };
| |_____^ help: you can reduce it to: `!returns_bool()` | |_____^ help: you can reduce it to: `{ !returns_bool() }`
error: aborting due to 16 previous errors error: aborting due to 16 previous errors