Skip to content

Simplify boolean expression in manual_assert #15368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions clippy_lints/src/manual_assert.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::{is_panic, root_macro_call};
use clippy_utils::{is_else_clause, is_parent_stmt, peel_blocks_with_stmt, span_extract_comment, sugg};
use clippy_utils::{higher, is_else_clause, is_parent_stmt, peel_blocks_with_stmt, span_extract_comment, sugg};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, UnOp};
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::declare_lint_pass;

Expand Down Expand Up @@ -35,7 +35,7 @@ declare_lint_pass!(ManualAssert => [MANUAL_ASSERT]);

impl<'tcx> LateLintPass<'tcx> for ManualAssert {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
if let ExprKind::If(cond, then, None) = expr.kind
if let Some(higher::If { cond, then, r#else: None }) = higher::If::hir(expr)
&& !matches!(cond.kind, ExprKind::Let(_))
&& !expr.span.from_expansion()
&& let then = peel_blocks_with_stmt(then)
Expand All @@ -51,19 +51,13 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
&& !is_else_clause(cx.tcx, expr)
{
let mut applicability = Applicability::MachineApplicable;
let cond = cond.peel_drop_temps();
let mut comments = span_extract_comment(cx.sess().source_map(), expr.span);
if !comments.is_empty() {
comments += "\n";
}
let (cond, not) = match cond.kind {
ExprKind::Unary(UnOp::Not, e) => (e, ""),
_ => (cond, "!"),
};
let cond_sugg =
sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability).maybe_paren();
let cond_sugg = !sugg::Sugg::hir_with_context(cx, cond, expr.span.ctxt(), "..", &mut applicability);
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
let sugg = format!("assert!({cond_sugg}, {format_args_snip}){semicolon}");
// we show to the user the suggestion without the comments, but when applying the fix, include the
// comments in the block
span_lint_and_then(
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/manual_assert.edition2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ LL - comment */
LL - /// Doc comment
LL - panic!("panic with comment") // comment after `panic!`
LL - }
LL + assert!(!(a > 2), "panic with comment");
LL + assert!(a <= 2, "panic with comment");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -186,7 +186,7 @@ LL - const BAR: () = if N == 0 {
LL -
LL - panic!()
LL - };
LL + const BAR: () = assert!(!(N == 0), );
LL + const BAR: () = assert!(N != 0, );
|

error: only a `panic!` in `if`-then statement
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/manual_assert.edition2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ LL - comment */
LL - /// Doc comment
LL - panic!("panic with comment") // comment after `panic!`
LL - }
LL + assert!(!(a > 2), "panic with comment");
LL + assert!(a <= 2, "panic with comment");
|

error: only a `panic!` in `if`-then statement
Expand All @@ -186,7 +186,7 @@ LL - const BAR: () = if N == 0 {
LL -
LL - panic!()
LL - };
LL + const BAR: () = assert!(!(N == 0), );
LL + const BAR: () = assert!(N != 0, );
|

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