Skip to content

Commit ea26a95

Browse files
committed
Auto merge of #4335 - phansch:fix_needless_bool_suggestion, r=flip1995
Fix needless_bool suggestion with if--else-if--else changelog: Fix `needless_bool` suggestion with if--else-if--else Closes #4334
2 parents a813c05 + cdfb72a commit ea26a95

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

clippy_lints/src/needless_bool.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) -> bool
118118
let parent_id = cx.tcx.hir().get_parent_node(expr.hir_id);
119119
let parent_node = cx.tcx.hir().get(parent_id);
120120

121-
if let rustc::hir::Node::Expr(e) = parent_node {
122-
if higher::if_block(&e).is_some() {
123-
return true;
124-
}
121+
match parent_node {
122+
rustc::hir::Node::Expr(e) => higher::if_block(&e).is_some(),
123+
rustc::hir::Node::Arm(e) => higher::if_block(&e.body).is_some(),
124+
_ => false,
125125
}
126-
127-
false
128126
}
129127

130128
declare_lint_pass!(BoolComparison => [BOOL_COMPARISON]);

tests/ui/needless_bool.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::needless_bool)]
2+
#![allow(unused, dead_code, clippy::no_effect)]
23

34
use std::cell::Cell;
45

tests/ui/needless_bool.stderr

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this if-then-else expression will always return true
2-
--> $DIR/needless_bool.rs:31:5
2+
--> $DIR/needless_bool.rs:32:5
33
|
44
LL | / if x {
55
LL | | true
@@ -11,7 +11,7 @@ LL | | };
1111
= note: `-D clippy::needless-bool` implied by `-D warnings`
1212

1313
error: this if-then-else expression will always return false
14-
--> $DIR/needless_bool.rs:36:5
14+
--> $DIR/needless_bool.rs:37:5
1515
|
1616
LL | / if x {
1717
LL | | false
@@ -21,7 +21,7 @@ LL | | };
2121
| |_____^
2222

2323
error: this if-then-else expression returns a bool literal
24-
--> $DIR/needless_bool.rs:41:5
24+
--> $DIR/needless_bool.rs:42:5
2525
|
2626
LL | / if x {
2727
LL | | true
@@ -31,7 +31,7 @@ LL | | };
3131
| |_____^ help: you can reduce it to: `x`
3232

3333
error: this if-then-else expression returns a bool literal
34-
--> $DIR/needless_bool.rs:46:5
34+
--> $DIR/needless_bool.rs:47:5
3535
|
3636
LL | / if x {
3737
LL | | false
@@ -41,7 +41,7 @@ LL | | };
4141
| |_____^ help: you can reduce it to: `!x`
4242

4343
error: this if-then-else expression returns a bool literal
44-
--> $DIR/needless_bool.rs:51:5
44+
--> $DIR/needless_bool.rs:52:5
4545
|
4646
LL | / if x && y {
4747
LL | | false
@@ -51,7 +51,7 @@ LL | | };
5151
| |_____^ help: you can reduce it to: `!(x && y)`
5252

5353
error: this if-then-else expression will always return true
54-
--> $DIR/needless_bool.rs:74:5
54+
--> $DIR/needless_bool.rs:75:5
5555
|
5656
LL | / if x {
5757
LL | | return true;
@@ -61,7 +61,7 @@ LL | | };
6161
| |_____^
6262

6363
error: this if-then-else expression will always return false
64-
--> $DIR/needless_bool.rs:83:5
64+
--> $DIR/needless_bool.rs:84:5
6565
|
6666
LL | / if x {
6767
LL | | return false;
@@ -71,7 +71,7 @@ LL | | };
7171
| |_____^
7272

7373
error: this if-then-else expression returns a bool literal
74-
--> $DIR/needless_bool.rs:92:5
74+
--> $DIR/needless_bool.rs:93:5
7575
|
7676
LL | / if x {
7777
LL | | return true;
@@ -81,7 +81,7 @@ LL | | };
8181
| |_____^ help: you can reduce it to: `return x`
8282

8383
error: this if-then-else expression returns a bool literal
84-
--> $DIR/needless_bool.rs:101:5
84+
--> $DIR/needless_bool.rs:102:5
8585
|
8686
LL | / if x && y {
8787
LL | | return true;
@@ -91,7 +91,7 @@ LL | | };
9191
| |_____^ help: you can reduce it to: `return x && y`
9292

9393
error: this if-then-else expression returns a bool literal
94-
--> $DIR/needless_bool.rs:110:5
94+
--> $DIR/needless_bool.rs:111:5
9595
|
9696
LL | / if x {
9797
LL | | return false;
@@ -101,7 +101,7 @@ LL | | };
101101
| |_____^ help: you can reduce it to: `return !x`
102102

103103
error: this if-then-else expression returns a bool literal
104-
--> $DIR/needless_bool.rs:119:5
104+
--> $DIR/needless_bool.rs:120:5
105105
|
106106
LL | / if x && y {
107107
LL | | return false;
@@ -111,41 +111,41 @@ LL | | };
111111
| |_____^ help: you can reduce it to: `return !(x && y)`
112112

113113
error: equality checks against true are unnecessary
114-
--> $DIR/needless_bool.rs:127:8
114+
--> $DIR/needless_bool.rs:128:8
115115
|
116116
LL | if x == true {};
117117
| ^^^^^^^^^ help: try simplifying it as shown: `x`
118118
|
119119
= note: `-D clippy::bool-comparison` implied by `-D warnings`
120120

121121
error: equality checks against false can be replaced by a negation
122-
--> $DIR/needless_bool.rs:131:8
122+
--> $DIR/needless_bool.rs:132:8
123123
|
124124
LL | if x == false {};
125125
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
126126

127127
error: equality checks against true are unnecessary
128-
--> $DIR/needless_bool.rs:141:8
128+
--> $DIR/needless_bool.rs:142:8
129129
|
130130
LL | if x == true {};
131131
| ^^^^^^^^^ help: try simplifying it as shown: `x`
132132

133133
error: equality checks against false can be replaced by a negation
134-
--> $DIR/needless_bool.rs:142:8
134+
--> $DIR/needless_bool.rs:143:8
135135
|
136136
LL | if x == false {};
137137
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
138138

139139
error: this if-then-else expression returns a bool literal
140-
--> $DIR/needless_bool.rs:151:12
140+
--> $DIR/needless_bool.rs:152:12
141141
|
142142
LL | } else if returns_bool() {
143143
| ____________^
144144
LL | | false
145145
LL | | } else {
146146
LL | | true
147147
LL | | };
148-
| |_____^ help: you can reduce it to: `!returns_bool()`
148+
| |_____^ help: you can reduce it to: `{ !returns_bool() }`
149149

150150
error: aborting due to 16 previous errors
151151

0 commit comments

Comments
 (0)