Skip to content

Commit df0499a

Browse files
bool_comparison: fix incorrect suggestion with >/< and macros (#15513)
Fixes rust-lang/rust-clippy#15497 changelog: [`bool_comparison`]: fix incorrect suggestion with `>`/`<` and macros
2 parents 4bb6a0e + 7697b61 commit df0499a

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,9 @@ fn check_comparison<'a, 'tcx>(
381381
suggest_bool_comparison(cx, binop_span, left_side, applicability, m, h);
382382
}),
383383
(None, None) => no_literal.map_or((), |(h, m)| {
384-
let left_side = Sugg::hir_with_applicability(cx, left_side, "..", &mut applicability);
385-
let right_side = Sugg::hir_with_applicability(cx, right_side, "..", &mut applicability);
384+
let left_side = Sugg::hir_with_context(cx, left_side, binop_span.ctxt(), "..", &mut applicability);
385+
let right_side =
386+
Sugg::hir_with_context(cx, right_side, binop_span.ctxt(), "..", &mut applicability);
386387
span_lint_and_sugg(
387388
cx,
388389
BOOL_COMPARISON,

tests/ui/bool_comparison.fixed

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ fn main() {
9191
};
9292
}
9393

94-
#[allow(dead_code)]
9594
fn issue3703() {
9695
struct Foo;
9796
impl PartialEq<bool> for Foo {
@@ -127,7 +126,6 @@ fn issue3703() {
127126
if false < Foo {}
128127
}
129128

130-
#[allow(dead_code)]
131129
fn issue4983() {
132130
let a = true;
133131
let b = false;
@@ -157,7 +155,6 @@ fn func() -> bool {
157155
true
158156
}
159157

160-
#[allow(dead_code)]
161158
fn issue3973() {
162159
// ok, don't lint on `cfg` invocation
163160
if false == cfg!(feature = "debugging") {}
@@ -199,3 +196,19 @@ fn issue9907() {
199196
let _ = ((1 < 2) != m!(func)) as usize;
200197
//~^ bool_comparison
201198
}
199+
200+
fn issue15497() {
201+
fn func() -> bool {
202+
true
203+
}
204+
205+
fn foo(x: bool) -> bool {
206+
x & !m!(func)
207+
//~^ bool_comparison
208+
}
209+
210+
fn bar(x: bool) -> bool {
211+
!x & m!(func)
212+
//~^ bool_comparison
213+
}
214+
}

tests/ui/bool_comparison.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ fn main() {
9191
};
9292
}
9393

94-
#[allow(dead_code)]
9594
fn issue3703() {
9695
struct Foo;
9796
impl PartialEq<bool> for Foo {
@@ -127,7 +126,6 @@ fn issue3703() {
127126
if false < Foo {}
128127
}
129128

130-
#[allow(dead_code)]
131129
fn issue4983() {
132130
let a = true;
133131
let b = false;
@@ -157,7 +155,6 @@ fn func() -> bool {
157155
true
158156
}
159157

160-
#[allow(dead_code)]
161158
fn issue3973() {
162159
// ok, don't lint on `cfg` invocation
163160
if false == cfg!(feature = "debugging") {}
@@ -199,3 +196,19 @@ fn issue9907() {
199196
let _ = ((1 < 2) == !m!(func)) as usize;
200197
//~^ bool_comparison
201198
}
199+
200+
fn issue15497() {
201+
fn func() -> bool {
202+
true
203+
}
204+
205+
fn foo(x: bool) -> bool {
206+
x > m!(func)
207+
//~^ bool_comparison
208+
}
209+
210+
fn bar(x: bool) -> bool {
211+
x < m!(func)
212+
//~^ bool_comparison
213+
}
214+
}

tests/ui/bool_comparison.stderr

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,70 +86,82 @@ LL | if x > y {
8686
| ^^^^^ help: try simplifying it as shown: `x & !y`
8787

8888
error: this comparison might be written more concisely
89-
--> tests/ui/bool_comparison.rs:135:8
89+
--> tests/ui/bool_comparison.rs:133:8
9090
|
9191
LL | if a == !b {};
9292
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9393

9494
error: this comparison might be written more concisely
95-
--> tests/ui/bool_comparison.rs:137:8
95+
--> tests/ui/bool_comparison.rs:135:8
9696
|
9797
LL | if !a == b {};
9898
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9999

100100
error: this comparison might be written more concisely
101-
--> tests/ui/bool_comparison.rs:142:8
101+
--> tests/ui/bool_comparison.rs:140:8
102102
|
103103
LL | if b == !a {};
104104
| ^^^^^^^ help: try simplifying it as shown: `b != a`
105105

106106
error: this comparison might be written more concisely
107-
--> tests/ui/bool_comparison.rs:144:8
107+
--> tests/ui/bool_comparison.rs:142:8
108108
|
109109
LL | if !b == a {};
110110
| ^^^^^^^ help: try simplifying it as shown: `b != a`
111111

112112
error: equality checks against false can be replaced by a negation
113-
--> tests/ui/bool_comparison.rs:169:8
113+
--> tests/ui/bool_comparison.rs:166:8
114114
|
115115
LL | if false == m!(func) {}
116116
| ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
117117

118118
error: equality checks against false can be replaced by a negation
119-
--> tests/ui/bool_comparison.rs:171:8
119+
--> tests/ui/bool_comparison.rs:168:8
120120
|
121121
LL | if m!(func) == false {}
122122
| ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
123123

124124
error: equality checks against true are unnecessary
125-
--> tests/ui/bool_comparison.rs:173:8
125+
--> tests/ui/bool_comparison.rs:170:8
126126
|
127127
LL | if true == m!(func) {}
128128
| ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `m!(func)`
129129

130130
error: equality checks against true are unnecessary
131-
--> tests/ui/bool_comparison.rs:175:8
131+
--> tests/ui/bool_comparison.rs:172:8
132132
|
133133
LL | if m!(func) == true {}
134134
| ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `m!(func)`
135135

136136
error: equality checks against false can be replaced by a negation
137-
--> tests/ui/bool_comparison.rs:193:14
137+
--> tests/ui/bool_comparison.rs:190:14
138138
|
139139
LL | let _ = ((1 < 2) == false) as usize;
140140
| ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `1 >= 2`
141141

142142
error: equality checks against false can be replaced by a negation
143-
--> tests/ui/bool_comparison.rs:195:14
143+
--> tests/ui/bool_comparison.rs:192:14
144144
|
145145
LL | let _ = (false == m!(func)) as usize;
146146
| ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
147147

148148
error: this comparison might be written more concisely
149-
--> tests/ui/bool_comparison.rs:199:14
149+
--> tests/ui/bool_comparison.rs:196:14
150150
|
151151
LL | let _ = ((1 < 2) == !m!(func)) as usize;
152152
| ^^^^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `(1 < 2) != m!(func)`
153153

154-
error: aborting due to 25 previous errors
154+
error: order comparisons between booleans can be simplified
155+
--> tests/ui/bool_comparison.rs:206:9
156+
|
157+
LL | x > m!(func)
158+
| ^^^^^^^^^^^^ help: try simplifying it as shown: `x & !m!(func)`
159+
160+
error: order comparisons between booleans can be simplified
161+
--> tests/ui/bool_comparison.rs:211:9
162+
|
163+
LL | x < m!(func)
164+
| ^^^^^^^^^^^^ help: try simplifying it as shown: `!x & m!(func)`
165+
166+
error: aborting due to 27 previous errors
155167

0 commit comments

Comments
 (0)