Skip to content

Commit f19e877

Browse files
committed
Account for new assert! desugaring in !condition suggestion
`rustc` is going to change the desugaring of `assert!` to be ```rust match condition { true => {} _ => panic!(), } ``` which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable.
1 parent 7dd3ecb commit f19e877

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

clippy_lints/src/bool_assert_comparison.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,24 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
130130

131131
let mut suggestions = vec![(name_span, non_eq_mac.to_string()), (lit_span, String::new())];
132132

133-
if bool_value ^ eq_macro {
134-
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else {
135-
return;
136-
};
137-
suggestions.push((non_lit_expr.span, (!sugg).to_string()));
133+
if let ty::Bool = non_lit_ty.kind() {
134+
if bool_value ^ eq_macro {
135+
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else {
136+
return;
137+
};
138+
suggestions.push((non_lit_expr.span, (!sugg).to_string()));
139+
}
140+
} else {
141+
// If we have a `value` that is *not* `bool` but that `!value` *is*, we suggest
142+
// `!!value`.
143+
suggestions.push((
144+
non_lit_expr.span.shrink_to_lo(),
145+
if bool_value ^ eq_macro {
146+
"!".to_string()
147+
} else {
148+
"!!".to_string()
149+
},
150+
));
138151
}
139152

140153
diag.multipart_suggestion(

tests/ui/bool_assert_comparison.fixed

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn main() {
9494
assert_eq!(a!(), "".is_empty());
9595
assert_eq!("".is_empty(), b!());
9696
assert_eq!(a, true);
97-
assert!(b);
97+
assert!(!!b);
9898
//~^ bool_assert_comparison
9999

100100
assert_ne!("a".len(), 1);
@@ -122,7 +122,7 @@ fn main() {
122122
debug_assert_eq!(a!(), "".is_empty());
123123
debug_assert_eq!("".is_empty(), b!());
124124
debug_assert_eq!(a, true);
125-
debug_assert!(b);
125+
debug_assert!(!!b);
126126
//~^ bool_assert_comparison
127127

128128
debug_assert_ne!("a".len(), 1);
@@ -167,7 +167,7 @@ fn main() {
167167

168168
use debug_assert_eq as renamed;
169169
renamed!(a, true);
170-
debug_assert!(b);
170+
debug_assert!(!!b);
171171
//~^ bool_assert_comparison
172172

173173
let non_copy = NonCopy;

tests/ui/bool_assert_comparison.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ LL | assert_eq!(b, true);
4545
help: replace it with `assert!(..)`
4646
|
4747
LL - assert_eq!(b, true);
48-
LL + assert!(b);
48+
LL + assert!(!!b);
4949
|
5050

5151
error: used `assert_ne!` with a literal bool
@@ -141,7 +141,7 @@ LL | debug_assert_eq!(b, true);
141141
help: replace it with `debug_assert!(..)`
142142
|
143143
LL - debug_assert_eq!(b, true);
144-
LL + debug_assert!(b);
144+
LL + debug_assert!(!!b);
145145
|
146146

147147
error: used `debug_assert_ne!` with a literal bool
@@ -297,7 +297,7 @@ LL | renamed!(b, true);
297297
help: replace it with `debug_assert!(..)`
298298
|
299299
LL - renamed!(b, true);
300-
LL + debug_assert!(b);
300+
LL + debug_assert!(!!b);
301301
|
302302

303303
error: used `assert_eq!` with a literal bool

0 commit comments

Comments
 (0)