You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: clippy_lints/src/manual_assert.rs
+7-4Lines changed: 7 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ use clippy_utils::macros::{root_macro_call, FormatArgsExpn};
4
4
use clippy_utils::source::snippet_with_applicability;
5
5
use clippy_utils::{peel_blocks_with_stmt, span_extract_comment, sugg};
6
6
use rustc_errors::Applicability;
7
-
use rustc_hir::{Expr,ExprKind};
7
+
use rustc_hir::{Expr,ExprKind,UnOp};
8
8
use rustc_lint::{LateContext,LateLintPass};
9
9
use rustc_session::{declare_lint_pass, declare_tool_lint};
10
10
use rustc_span::sym;
@@ -55,9 +55,12 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
55
55
if !comments.is_empty(){
56
56
comments += "\n";
57
57
}
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});");
61
64
// we show to the user the suggestion without the comments, but when applicating the fix, include the comments in the block
0 commit comments