Skip to content

Commit 62f60e1

Browse files
committed
No lint with cfg! and fix sugg for macro in needless_bool lint
1 parent 6bfc19c commit 62f60e1

File tree

4 files changed

+135
-23
lines changed

4 files changed

+135
-23
lines changed

clippy_lints/src/needless_bool.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//! This lint is **warn** by default
44
55
use crate::utils::sugg::Sugg;
6-
use crate::utils::{higher, parent_node_is_if_expr, snippet_with_applicability, span_lint, span_lint_and_sugg};
6+
use crate::utils::{
7+
higher, is_expn_of, parent_node_is_if_expr, snippet_with_applicability, span_lint, span_lint_and_sugg,
8+
};
79
use if_chain::if_chain;
810
use rustc_ast::ast::LitKind;
911
use rustc_errors::Applicability;
@@ -233,6 +235,9 @@ fn check_comparison<'a, 'tcx>(
233235
cx.typeck_results().expr_ty(left_side),
234236
cx.typeck_results().expr_ty(right_side),
235237
);
238+
if is_expn_of(left_side.span, "cfg").is_some() || is_expn_of(right_side.span, "cfg").is_some() {
239+
return;
240+
}
236241
if l_ty.is_bool() && r_ty.is_bool() {
237242
let mut applicability = Applicability::MachineApplicable;
238243

@@ -295,7 +300,14 @@ fn suggest_bool_comparison<'a, 'tcx>(
295300
message: &str,
296301
conv_hint: impl FnOnce(Sugg<'a>) -> Sugg<'a>,
297302
) {
298-
let hint = Sugg::hir_with_applicability(cx, expr, "..", &mut applicability);
303+
let hint = if expr.span.from_expansion() {
304+
if applicability != Applicability::Unspecified {
305+
applicability = Applicability::MaybeIncorrect;
306+
}
307+
Sugg::hir_with_macro_callsite(cx, expr, "..")
308+
} else {
309+
Sugg::hir_with_applicability(cx, expr, "..", &mut applicability)
310+
};
299311
span_lint_and_sugg(
300312
cx,
301313
BOOL_COMPARISON,

tests/ui/bool_comparison.fixed

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-rustfix
22

3-
#[warn(clippy::bool_comparison)]
3+
#![warn(clippy::bool_comparison)]
4+
45
fn main() {
56
let x = true;
67
if x {
@@ -127,3 +128,40 @@ fn issue4983() {
127128
if b == a {};
128129
if !b == !a {};
129130
}
131+
132+
macro_rules! m {
133+
($func:ident) => {
134+
$func()
135+
};
136+
}
137+
138+
fn func() -> bool {
139+
true
140+
}
141+
142+
#[allow(dead_code)]
143+
fn issue3973() {
144+
// ok, don't lint on `cfg` invocation
145+
if false == cfg!(feature = "debugging") {}
146+
if cfg!(feature = "debugging") == false {}
147+
if true == cfg!(feature = "debugging") {}
148+
if cfg!(feature = "debugging") == true {}
149+
150+
// lint, could be simplified
151+
if !m!(func) {}
152+
if !m!(func) {}
153+
if m!(func) {}
154+
if m!(func) {}
155+
156+
// no lint with a variable
157+
let is_debug = false;
158+
if is_debug == cfg!(feature = "debugging") {}
159+
if cfg!(feature = "debugging") == is_debug {}
160+
if is_debug == m!(func) {}
161+
if m!(func) == is_debug {}
162+
let is_debug = true;
163+
if is_debug == cfg!(feature = "debugging") {}
164+
if cfg!(feature = "debugging") == is_debug {}
165+
if is_debug == m!(func) {}
166+
if m!(func) == is_debug {}
167+
}

tests/ui/bool_comparison.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-rustfix
22

3-
#[warn(clippy::bool_comparison)]
3+
#![warn(clippy::bool_comparison)]
4+
45
fn main() {
56
let x = true;
67
if x == true {
@@ -127,3 +128,40 @@ fn issue4983() {
127128
if b == a {};
128129
if !b == !a {};
129130
}
131+
132+
macro_rules! m {
133+
($func:ident) => {
134+
$func()
135+
};
136+
}
137+
138+
fn func() -> bool {
139+
true
140+
}
141+
142+
#[allow(dead_code)]
143+
fn issue3973() {
144+
// ok, don't lint on `cfg` invocation
145+
if false == cfg!(feature = "debugging") {}
146+
if cfg!(feature = "debugging") == false {}
147+
if true == cfg!(feature = "debugging") {}
148+
if cfg!(feature = "debugging") == true {}
149+
150+
// lint, could be simplified
151+
if false == m!(func) {}
152+
if m!(func) == false {}
153+
if true == m!(func) {}
154+
if m!(func) == true {}
155+
156+
// no lint with a variable
157+
let is_debug = false;
158+
if is_debug == cfg!(feature = "debugging") {}
159+
if cfg!(feature = "debugging") == is_debug {}
160+
if is_debug == m!(func) {}
161+
if m!(func) == is_debug {}
162+
let is_debug = true;
163+
if is_debug == cfg!(feature = "debugging") {}
164+
if cfg!(feature = "debugging") == is_debug {}
165+
if is_debug == m!(func) {}
166+
if m!(func) == is_debug {}
167+
}

tests/ui/bool_comparison.stderr

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,136 @@
11
error: equality checks against true are unnecessary
2-
--> $DIR/bool_comparison.rs:6:8
2+
--> $DIR/bool_comparison.rs:7:8
33
|
44
LL | if x == true {
55
| ^^^^^^^^^ help: try simplifying it as shown: `x`
66
|
77
= note: `-D clippy::bool-comparison` implied by `-D warnings`
88

99
error: equality checks against false can be replaced by a negation
10-
--> $DIR/bool_comparison.rs:11:8
10+
--> $DIR/bool_comparison.rs:12:8
1111
|
1212
LL | if x == false {
1313
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
1414

1515
error: equality checks against true are unnecessary
16-
--> $DIR/bool_comparison.rs:16:8
16+
--> $DIR/bool_comparison.rs:17:8
1717
|
1818
LL | if true == x {
1919
| ^^^^^^^^^ help: try simplifying it as shown: `x`
2020

2121
error: equality checks against false can be replaced by a negation
22-
--> $DIR/bool_comparison.rs:21:8
22+
--> $DIR/bool_comparison.rs:22:8
2323
|
2424
LL | if false == x {
2525
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
2626

2727
error: inequality checks against true can be replaced by a negation
28-
--> $DIR/bool_comparison.rs:26:8
28+
--> $DIR/bool_comparison.rs:27:8
2929
|
3030
LL | if x != true {
3131
| ^^^^^^^^^ help: try simplifying it as shown: `!x`
3232

3333
error: inequality checks against false are unnecessary
34-
--> $DIR/bool_comparison.rs:31:8
34+
--> $DIR/bool_comparison.rs:32:8
3535
|
3636
LL | if x != false {
3737
| ^^^^^^^^^^ help: try simplifying it as shown: `x`
3838

3939
error: inequality checks against true can be replaced by a negation
40-
--> $DIR/bool_comparison.rs:36:8
40+
--> $DIR/bool_comparison.rs:37:8
4141
|
4242
LL | if true != x {
4343
| ^^^^^^^^^ help: try simplifying it as shown: `!x`
4444

4545
error: inequality checks against false are unnecessary
46-
--> $DIR/bool_comparison.rs:41:8
46+
--> $DIR/bool_comparison.rs:42:8
4747
|
4848
LL | if false != x {
4949
| ^^^^^^^^^^ help: try simplifying it as shown: `x`
5050

5151
error: less than comparison against true can be replaced by a negation
52-
--> $DIR/bool_comparison.rs:46:8
52+
--> $DIR/bool_comparison.rs:47:8
5353
|
5454
LL | if x < true {
5555
| ^^^^^^^^ help: try simplifying it as shown: `!x`
5656

5757
error: greater than checks against false are unnecessary
58-
--> $DIR/bool_comparison.rs:51:8
58+
--> $DIR/bool_comparison.rs:52:8
5959
|
6060
LL | if false < x {
6161
| ^^^^^^^^^ help: try simplifying it as shown: `x`
6262

6363
error: greater than checks against false are unnecessary
64-
--> $DIR/bool_comparison.rs:56:8
64+
--> $DIR/bool_comparison.rs:57:8
6565
|
6666
LL | if x > false {
6767
| ^^^^^^^^^ help: try simplifying it as shown: `x`
6868

6969
error: less than comparison against true can be replaced by a negation
70-
--> $DIR/bool_comparison.rs:61:8
70+
--> $DIR/bool_comparison.rs:62:8
7171
|
7272
LL | if true > x {
7373
| ^^^^^^^^ help: try simplifying it as shown: `!x`
7474

7575
error: order comparisons between booleans can be simplified
76-
--> $DIR/bool_comparison.rs:67:8
76+
--> $DIR/bool_comparison.rs:68:8
7777
|
7878
LL | if x < y {
7979
| ^^^^^ help: try simplifying it as shown: `!x & y`
8080

8181
error: order comparisons between booleans can be simplified
82-
--> $DIR/bool_comparison.rs:72:8
82+
--> $DIR/bool_comparison.rs:73:8
8383
|
8484
LL | if x > y {
8585
| ^^^^^ help: try simplifying it as shown: `x & !y`
8686

8787
error: this comparison might be written more concisely
88-
--> $DIR/bool_comparison.rs:120:8
88+
--> $DIR/bool_comparison.rs:121:8
8989
|
9090
LL | if a == !b {};
9191
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9292

9393
error: this comparison might be written more concisely
94-
--> $DIR/bool_comparison.rs:121:8
94+
--> $DIR/bool_comparison.rs:122:8
9595
|
9696
LL | if !a == b {};
9797
| ^^^^^^^ help: try simplifying it as shown: `a != b`
9898

9999
error: this comparison might be written more concisely
100-
--> $DIR/bool_comparison.rs:125:8
100+
--> $DIR/bool_comparison.rs:126:8
101101
|
102102
LL | if b == !a {};
103103
| ^^^^^^^ help: try simplifying it as shown: `b != a`
104104

105105
error: this comparison might be written more concisely
106-
--> $DIR/bool_comparison.rs:126:8
106+
--> $DIR/bool_comparison.rs:127:8
107107
|
108108
LL | if !b == a {};
109109
| ^^^^^^^ help: try simplifying it as shown: `b != a`
110110

111-
error: aborting due to 18 previous errors
111+
error: equality checks against false can be replaced by a negation
112+
--> $DIR/bool_comparison.rs:151:8
113+
|
114+
LL | if false == m!(func) {}
115+
| ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
116+
117+
error: equality checks against false can be replaced by a negation
118+
--> $DIR/bool_comparison.rs:152:8
119+
|
120+
LL | if m!(func) == false {}
121+
| ^^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `!m!(func)`
122+
123+
error: equality checks against true are unnecessary
124+
--> $DIR/bool_comparison.rs:153:8
125+
|
126+
LL | if true == m!(func) {}
127+
| ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `m!(func)`
128+
129+
error: equality checks against true are unnecessary
130+
--> $DIR/bool_comparison.rs:154:8
131+
|
132+
LL | if m!(func) == true {}
133+
| ^^^^^^^^^^^^^^^^ help: try simplifying it as shown: `m!(func)`
134+
135+
error: aborting due to 22 previous errors
112136

0 commit comments

Comments
 (0)