Skip to content

Commit a35734c

Browse files
committed
revert manual_assert suggestion refactor
Because `Sugg` helper does not simplify multiple negations
1 parent 2c04c1a commit a35734c

5 files changed

+15
-12
lines changed

clippy_lints/src/manual_assert.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::macros::{root_macro_call, FormatArgsExpn};
44
use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::{peel_blocks_with_stmt, span_extract_comment, sugg};
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Expr, ExprKind};
7+
use rustc_hir::{Expr, ExprKind, UnOp};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
1010
use rustc_span::sym;
@@ -55,9 +55,12 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
5555
if !comments.is_empty() {
5656
comments += "\n";
5757
}
58-
// we need to negate the <cond> expression because `assert!` panics when <cond> is `false`, wherease original pattern panicked when evaluating to `true`
59-
let cond_sugg = !sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability);
60-
let sugg = format!("assert!({cond_sugg}, {format_args_snip});");
58+
let (cond, not) = match cond.kind {
59+
ExprKind::Unary(UnOp::Not, e) => (e, ""),
60+
_ => (cond, "!"),
61+
};
62+
let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par();
63+
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip});");
6164
// we show to the user the suggestion without the comments, but when applicating the fix, include the comments in the block
6265
span_lint_and_then(
6366
cx,

tests/ui/manual_assert.edition2018.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fn main() {
2828
{
2929
panic!("qaqaq{:?}", a);
3030
}
31-
assert!(!(!a.is_empty()), "qaqaq{:?}", a);
32-
assert!(!(!a.is_empty()), "qwqwq");
31+
assert!(a.is_empty(), "qaqaq{:?}", a);
32+
assert!(a.is_empty(), "qwqwq");
3333
if a.len() == 3 {
3434
println!("qwq");
3535
println!("qwq");

tests/ui/manual_assert.edition2018.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | | }
99
= note: `-D clippy::manual-assert` implied by `-D warnings`
1010
help: try instead
1111
|
12-
LL | assert!(!(!a.is_empty()), "qaqaq{:?}", a);
12+
LL | assert!(a.is_empty(), "qaqaq{:?}", a);
1313
|
1414

1515
error: only a `panic!` in `if`-then statement
@@ -22,7 +22,7 @@ LL | | }
2222
|
2323
help: try instead
2424
|
25-
LL | assert!(!(!a.is_empty()), "qwqwq");
25+
LL | assert!(a.is_empty(), "qwqwq");
2626
|
2727

2828
error: only a `panic!` in `if`-then statement

tests/ui/manual_assert.edition2021.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fn main() {
2828
{
2929
panic!("qaqaq{:?}", a);
3030
}
31-
assert!(!(!a.is_empty()), "qaqaq{:?}", a);
32-
assert!(!(!a.is_empty()), "qwqwq");
31+
assert!(a.is_empty(), "qaqaq{:?}", a);
32+
assert!(a.is_empty(), "qwqwq");
3333
if a.len() == 3 {
3434
println!("qwq");
3535
println!("qwq");

tests/ui/manual_assert.edition2021.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | | }
99
= note: `-D clippy::manual-assert` implied by `-D warnings`
1010
help: try instead
1111
|
12-
LL | assert!(!(!a.is_empty()), "qaqaq{:?}", a);
12+
LL | assert!(a.is_empty(), "qaqaq{:?}", a);
1313
|
1414

1515
error: only a `panic!` in `if`-then statement
@@ -22,7 +22,7 @@ LL | | }
2222
|
2323
help: try instead
2424
|
25-
LL | assert!(!(!a.is_empty()), "qwqwq");
25+
LL | assert!(a.is_empty(), "qwqwq");
2626
|
2727

2828
error: only a `panic!` in `if`-then statement

0 commit comments

Comments
 (0)