Skip to content

Commit b144c7f

Browse files
bors[bot]Giorgio Gambino
and
Giorgio Gambino
committed
Merge #3370
3370: bool_comparison triggers 3 times on same code r=phansch a=mrbuzz Fix #3335 Co-authored-by: Giorgio Gambino <[email protected]>
2 parents 2362b3a + c0c1f1f commit b144c7f

File tree

3 files changed

+99
-25
lines changed

3 files changed

+99
-25
lines changed

clippy_lints/src/needless_bool.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::rustc::{declare_tool_lint, lint_array};
1717
use crate::rustc::hir::*;
1818
use crate::syntax::ast::LitKind;
1919
use crate::syntax::source_map::Spanned;
20-
use crate::utils::{snippet, span_lint, span_lint_and_sugg};
20+
use crate::utils::{in_macro, snippet, span_lint, span_lint_and_sugg};
2121
use crate::utils::sugg::Sugg;
2222

2323
/// **What it does:** Checks for expressions of the form `if c { true } else {
@@ -133,6 +133,9 @@ impl LintPass for BoolComparison {
133133

134134
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
135135
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
136+
if in_macro(e.span) {
137+
return;
138+
}
136139
use self::Expression::*;
137140
if let ExprKind::Binary(Spanned { node: BinOpKind::Eq, .. }, ref left_side, ref right_side) = e.node {
138141
match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {

tests/ui/needless_bool.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,31 @@
88
// except according to those terms.
99

1010

11+
#![warn(clippy::needless_bool)]
1112

13+
use std::cell::Cell;
1214

13-
#![warn(clippy::needless_bool)]
15+
macro_rules! bool_comparison_trigger {
16+
($($i:ident: $def:expr, $stb:expr );+ $(;)*) => (
17+
18+
#[derive(Clone)]
19+
pub struct Trigger {
20+
$($i: (Cell<bool>, bool, bool)),+
21+
}
22+
23+
#[allow(dead_code)]
24+
impl Trigger {
25+
pub fn trigger(&self, key: &str) -> bool {
26+
$(
27+
if let stringify!($i) = key {
28+
return self.$i.1 && self.$i.2 == $def;
29+
}
30+
)+
31+
false
32+
}
33+
}
34+
)
35+
}
1436

1537
#[allow(clippy::if_same_then_else)]
1638
fn main() {
@@ -28,6 +50,9 @@ fn main() {
2850
bool_ret5(x, x);
2951
bool_ret4(x);
3052
bool_ret6(x, x);
53+
needless_bool(x);
54+
needless_bool2(x);
55+
needless_bool3(x);
3156
}
3257

3358
#[allow(clippy::if_same_then_else, clippy::needless_return)]
@@ -59,3 +84,23 @@ fn bool_ret4(x: bool) -> bool {
5984
fn bool_ret6(x: bool, y: bool) -> bool {
6085
if x && y { return false } else { return true };
6186
}
87+
88+
fn needless_bool(x: bool) {
89+
if x == true { };
90+
}
91+
92+
fn needless_bool2(x: bool) {
93+
if x == false { };
94+
}
95+
96+
fn needless_bool3(x: bool) {
97+
98+
bool_comparison_trigger! {
99+
test_one: false, false;
100+
test_three: false, false;
101+
test_two: true, true;
102+
}
103+
104+
if x == true { };
105+
if x == false { };
106+
}

tests/ui/needless_bool.stderr

+49-23
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,96 @@
11
error: this if-then-else expression will always return true
2-
--> $DIR/needless_bool.rs:19:5
2+
--> $DIR/needless_bool.rs:41:5
33
|
4-
19 | if x { true } else { true };
4+
41 | if x { true } else { true };
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::needless-bool` implied by `-D warnings`
88

99
error: this if-then-else expression will always return false
10-
--> $DIR/needless_bool.rs:20:5
10+
--> $DIR/needless_bool.rs:42:5
1111
|
12-
20 | if x { false } else { false };
12+
42 | if x { false } else { false };
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: this if-then-else expression returns a bool literal
16-
--> $DIR/needless_bool.rs:21:5
16+
--> $DIR/needless_bool.rs:43:5
1717
|
18-
21 | if x { true } else { false };
18+
43 | if x { true } else { false };
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `x`
2020

2121
error: this if-then-else expression returns a bool literal
22-
--> $DIR/needless_bool.rs:22:5
22+
--> $DIR/needless_bool.rs:44:5
2323
|
24-
22 | if x { false } else { true };
24+
44 | if x { false } else { true };
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `!x`
2626

2727
error: this if-then-else expression returns a bool literal
28-
--> $DIR/needless_bool.rs:23:5
28+
--> $DIR/needless_bool.rs:45:5
2929
|
30-
23 | if x && y { false } else { true };
30+
45 | if x && y { false } else { true };
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `!(x && y)`
3232

3333
error: this if-then-else expression will always return true
34-
--> $DIR/needless_bool.rs:35:5
34+
--> $DIR/needless_bool.rs:60:5
3535
|
36-
35 | if x { return true } else { return true };
36+
60 | if x { return true } else { return true };
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: this if-then-else expression will always return false
40-
--> $DIR/needless_bool.rs:40:5
40+
--> $DIR/needless_bool.rs:65:5
4141
|
42-
40 | if x { return false } else { return false };
42+
65 | if x { return false } else { return false };
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

4545
error: this if-then-else expression returns a bool literal
46-
--> $DIR/needless_bool.rs:45:5
46+
--> $DIR/needless_bool.rs:70:5
4747
|
48-
45 | if x { return true } else { return false };
48+
70 | if x { return true } else { return false };
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `return x`
5050

5151
error: this if-then-else expression returns a bool literal
52-
--> $DIR/needless_bool.rs:50:5
52+
--> $DIR/needless_bool.rs:75:5
5353
|
54-
50 | if x && y { return true } else { return false };
54+
75 | if x && y { return true } else { return false };
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `return x && y`
5656

5757
error: this if-then-else expression returns a bool literal
58-
--> $DIR/needless_bool.rs:55:5
58+
--> $DIR/needless_bool.rs:80:5
5959
|
60-
55 | if x { return false } else { return true };
60+
80 | if x { return false } else { return true };
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `return !x`
6262

6363
error: this if-then-else expression returns a bool literal
64-
--> $DIR/needless_bool.rs:60:5
64+
--> $DIR/needless_bool.rs:85:5
6565
|
66-
60 | if x && y { return false } else { return true };
66+
85 | if x && y { return false } else { return true };
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `return !(x && y)`
6868

69-
error: aborting due to 11 previous errors
69+
error: equality checks against true are unnecessary
70+
--> $DIR/needless_bool.rs:89:7
71+
|
72+
89 | if x == true { };
73+
| ^^^^^^^^^^ help: try simplifying it as shown: `x`
74+
|
75+
= note: `-D clippy::bool-comparison` implied by `-D warnings`
76+
77+
error: equality checks against false can be replaced by a negation
78+
--> $DIR/needless_bool.rs:93:7
79+
|
80+
93 | if x == false { };
81+
| ^^^^^^^^^^^ help: try simplifying it as shown: `!x`
82+
83+
error: equality checks against true are unnecessary
84+
--> $DIR/needless_bool.rs:104:8
85+
|
86+
104 | if x == true { };
87+
| ^^^^^^^^^ help: try simplifying it as shown: `x`
88+
89+
error: equality checks against false can be replaced by a negation
90+
--> $DIR/needless_bool.rs:105:8
91+
|
92+
105 | if x == false { };
93+
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
94+
95+
error: aborting due to 15 previous errors
7096

0 commit comments

Comments
 (0)