Skip to content

Commit 0d358fd

Browse files
committed
Fix clippy after changes to assert! desugaring
1 parent ac00b51 commit 0d358fd

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

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

+19-5
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,27 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
129129

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

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

152+
139153
diag.multipart_suggestion(
140154
format!("replace it with `{non_eq_mac}!(..)`"),
141155
suggestions,

src/tools/clippy/clippy_lints/src/missing_asserts_for_indexing.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_ast::{LitKind, RangeLimits};
1010
use rustc_data_structures::packed::Pu128;
1111
use rustc_data_structures::unhash::UnhashMap;
1212
use rustc_errors::{Applicability, Diag};
13-
use rustc_hir::{BinOp, Block, Body, Expr, ExprKind, UnOp};
13+
use rustc_hir::{BinOp, Block, Body, Expr, ExprKind, UnOp, StmtKind};
1414
use rustc_lint::{LateContext, LateLintPass};
1515
use rustc_session::declare_lint_pass;
1616
use rustc_span::source_map::Spanned;
@@ -136,6 +136,10 @@ fn assert_len_expr<'hir>(
136136
) -> Option<(LengthComparison, usize, &'hir Expr<'hir>)> {
137137
if let Some(higher::If { cond, then, .. }) = higher::If::hir(expr)
138138
&& let ExprKind::Unary(UnOp::Not, condition) = &cond.kind
139+
&& let ExprKind::Block(block, None) = condition.kind
140+
&& let [local] = block.stmts
141+
&& let StmtKind::Let(local) = local.kind
142+
&& let Some(condition) = local.init
139143
&& let ExprKind::Binary(bin_op, left, right) = &condition.kind
140144

141145
&& let Some((cmp, asserted_len, slice_len)) = len_comparison(*bin_op, left, right)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn main() {
9191
assert_eq!(a!(), "".is_empty());
9292
assert_eq!("".is_empty(), b!());
9393
assert_eq!(a, true);
94-
assert!(b);
94+
assert!(!!b);
9595

9696
assert_ne!("a".len(), 1);
9797
assert!("a".is_empty());
@@ -111,7 +111,7 @@ fn main() {
111111
debug_assert_eq!(a!(), "".is_empty());
112112
debug_assert_eq!("".is_empty(), b!());
113113
debug_assert_eq!(a, true);
114-
debug_assert!(b);
114+
debug_assert!(!!b);
115115

116116
debug_assert_ne!("a".len(), 1);
117117
debug_assert!("a".is_empty());
@@ -143,7 +143,7 @@ fn main() {
143143

144144
use debug_assert_eq as renamed;
145145
renamed!(a, true);
146-
debug_assert!(b);
146+
debug_assert!(!!b);
147147

148148
let non_copy = NonCopy;
149149
assert_eq!(non_copy, true);

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

+3-3
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)