Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix needless_bool suggestion with if--else-if--else #4335

Merged
merged 2 commits into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions clippy_lints/src/needless_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,11 @@ 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]);
Expand Down
1 change: 1 addition & 0 deletions tests/ui/needless_bool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::needless_bool)]
#![allow(unused, dead_code, clippy::no_effect)]

use std::cell::Cell;

Expand Down
34 changes: 17 additions & 17 deletions tests/ui/needless_bool.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -111,41 +111,41 @@ 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`
|
= 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() {
| ____________^
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