Skip to content

Commit 84cce89

Browse files
authored
Rollup merge of #145273 - estebank:not-not, r=samueltardieu
Account for new `assert!` desugaring in `!condition` suggestion `rustc` in #122661 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. Transposing rust-lang/rust-clippy#15453 to the rustc repo. r? ```@samueltardieu```
2 parents 30bff95 + 86853b3 commit 84cce89

File tree

4 files changed

+144
-56
lines changed

4 files changed

+144
-56
lines changed

src/tools/clippy/clippy_lints/src/bool_assert_comparison.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,22 @@ 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;
133+
if let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) {
134+
let sugg = if bool_value ^ eq_macro {
135+
!sugg.maybe_paren()
136+
} else if ty::Bool == *non_lit_ty.kind() {
137+
sugg
138+
} else {
139+
!!sugg.maybe_paren()
136140
};
137-
suggestions.push((non_lit_expr.span, (!sugg).to_string()));
138-
}
141+
suggestions.push((non_lit_expr.span, sugg.to_string()));
139142

140-
diag.multipart_suggestion(
141-
format!("replace it with `{non_eq_mac}!(..)`"),
142-
suggestions,
143-
Applicability::MachineApplicable,
144-
);
143+
diag.multipart_suggestion(
144+
format!("replace it with `{non_eq_mac}!(..)`"),
145+
suggestions,
146+
Applicability::MachineApplicable,
147+
);
148+
}
145149
},
146150
);
147151
}

src/tools/clippy/tests/ui/bool_assert_comparison.fixed

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
22
#![warn(clippy::bool_assert_comparison)]
33

4-
use std::ops::Not;
4+
use std::ops::{Add, Not};
55

66
macro_rules! a {
77
() => {
@@ -62,6 +62,14 @@ impl Not for ImplNotTraitWithBool {
6262
}
6363
}
6464

65+
impl Add for ImplNotTraitWithBool {
66+
type Output = Self;
67+
68+
fn add(self, other: Self) -> Self::Output {
69+
self
70+
}
71+
}
72+
6573
#[derive(Debug)]
6674
struct NonCopy;
6775

@@ -94,7 +102,7 @@ fn main() {
94102
assert_eq!(a!(), "".is_empty());
95103
assert_eq!("".is_empty(), b!());
96104
assert_eq!(a, true);
97-
assert!(b);
105+
assert!(!!b);
98106
//~^ bool_assert_comparison
99107

100108
assert_ne!("a".len(), 1);
@@ -122,7 +130,7 @@ fn main() {
122130
debug_assert_eq!(a!(), "".is_empty());
123131
debug_assert_eq!("".is_empty(), b!());
124132
debug_assert_eq!(a, true);
125-
debug_assert!(b);
133+
debug_assert!(!!b);
126134
//~^ bool_assert_comparison
127135

128136
debug_assert_ne!("a".len(), 1);
@@ -167,7 +175,7 @@ fn main() {
167175

168176
use debug_assert_eq as renamed;
169177
renamed!(a, true);
170-
debug_assert!(b);
178+
debug_assert!(!!b);
171179
//~^ bool_assert_comparison
172180

173181
let non_copy = NonCopy;
@@ -199,4 +207,12 @@ fn main() {
199207
//~^ bool_assert_comparison
200208
debug_assert!(!"requires negation".is_empty());
201209
//~^ bool_assert_comparison
210+
assert!(!b);
211+
//~^ bool_assert_comparison
212+
assert!(!(!b));
213+
//~^ bool_assert_comparison
214+
assert!(!!(b + b));
215+
//~^ bool_assert_comparison
216+
assert!(!(b + b));
217+
//~^ bool_assert_comparison
202218
}

src/tools/clippy/tests/ui/bool_assert_comparison.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
22
#![warn(clippy::bool_assert_comparison)]
33

4-
use std::ops::Not;
4+
use std::ops::{Add, Not};
55

66
macro_rules! a {
77
() => {
@@ -62,6 +62,14 @@ impl Not for ImplNotTraitWithBool {
6262
}
6363
}
6464

65+
impl Add for ImplNotTraitWithBool {
66+
type Output = Self;
67+
68+
fn add(self, other: Self) -> Self::Output {
69+
self
70+
}
71+
}
72+
6573
#[derive(Debug)]
6674
struct NonCopy;
6775

@@ -199,4 +207,12 @@ fn main() {
199207
//~^ bool_assert_comparison
200208
debug_assert_eq!("requires negation".is_empty(), false);
201209
//~^ bool_assert_comparison
210+
assert_eq!(!b, true);
211+
//~^ bool_assert_comparison
212+
assert_eq!(!b, false);
213+
//~^ bool_assert_comparison
214+
assert_eq!(b + b, true);
215+
//~^ bool_assert_comparison
216+
assert_eq!(b + b, false);
217+
//~^ bool_assert_comparison
202218
}

0 commit comments

Comments
 (0)