diff --git a/clippy_lints/src/manual_assert.rs b/clippy_lints/src/manual_assert.rs index 4f6a2cf017c..76edbe8b755 100644 --- a/clippy_lints/src/manual_assert.rs +++ b/clippy_lints/src/manual_assert.rs @@ -1,7 +1,7 @@ use crate::rustc_lint::LintContext; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::macros::root_macro_call; -use clippy_utils::{is_else_clause, peel_blocks_with_stmt, span_extract_comment, sugg}; +use clippy_utils::{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_lint::{LateContext, LateLintPass}; @@ -63,7 +63,8 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { _ => (cond, "!"), }; let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par(); - let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip});"); + let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" }; + let sugg = format!("assert!({not}{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( diff --git a/tests/ui/manual_assert.edition2018.fixed b/tests/ui/manual_assert.edition2018.fixed index 75beedfa450..75835780801 100644 --- a/tests/ui/manual_assert.edition2018.fixed +++ b/tests/ui/manual_assert.edition2018.fixed @@ -66,3 +66,11 @@ fn issue7730(a: u8) { // comment after `panic!` assert!(!(a > 2), "panic with comment"); } + +fn issue12505() { + struct Foo(T); + + impl Foo { + const BAR: () = assert!(!(N == 0), ); + } +} diff --git a/tests/ui/manual_assert.edition2018.stderr b/tests/ui/manual_assert.edition2018.stderr index 57015933d40..1eebe1bfe17 100644 --- a/tests/ui/manual_assert.edition2018.stderr +++ b/tests/ui/manual_assert.edition2018.stderr @@ -82,5 +82,14 @@ help: try instead LL | assert!(!(a > 2), "panic with comment"); | -error: aborting due to 9 previous errors +error: only a `panic!` in `if`-then statement + --> tests/ui/manual_assert.rs:91:25 + | +LL | const BAR: () = if N == 0 { + | _________________________^ +LL | | panic!() +LL | | }; + | |_________^ help: try instead: `assert!(!(N == 0), )` + +error: aborting due to 10 previous errors diff --git a/tests/ui/manual_assert.edition2021.fixed b/tests/ui/manual_assert.edition2021.fixed index 75beedfa450..75835780801 100644 --- a/tests/ui/manual_assert.edition2021.fixed +++ b/tests/ui/manual_assert.edition2021.fixed @@ -66,3 +66,11 @@ fn issue7730(a: u8) { // comment after `panic!` assert!(!(a > 2), "panic with comment"); } + +fn issue12505() { + struct Foo(T); + + impl Foo { + const BAR: () = assert!(!(N == 0), ); + } +} diff --git a/tests/ui/manual_assert.edition2021.stderr b/tests/ui/manual_assert.edition2021.stderr index 57015933d40..1eebe1bfe17 100644 --- a/tests/ui/manual_assert.edition2021.stderr +++ b/tests/ui/manual_assert.edition2021.stderr @@ -82,5 +82,14 @@ help: try instead LL | assert!(!(a > 2), "panic with comment"); | -error: aborting due to 9 previous errors +error: only a `panic!` in `if`-then statement + --> tests/ui/manual_assert.rs:91:25 + | +LL | const BAR: () = if N == 0 { + | _________________________^ +LL | | panic!() +LL | | }; + | |_________^ help: try instead: `assert!(!(N == 0), )` + +error: aborting due to 10 previous errors diff --git a/tests/ui/manual_assert.rs b/tests/ui/manual_assert.rs index 5979496ca83..363bafdf05d 100644 --- a/tests/ui/manual_assert.rs +++ b/tests/ui/manual_assert.rs @@ -83,3 +83,13 @@ fn issue7730(a: u8) { panic!("panic with comment") // comment after `panic!` } } + +fn issue12505() { + struct Foo(T); + + impl Foo { + const BAR: () = if N == 0 { + panic!() + }; + } +}