manual_assert: do not add extra semicolon

This commit is contained in:
Samuel Tardieu 2024-03-23 01:32:25 +01:00
parent 6b12829943
commit c137c78ba2
6 changed files with 49 additions and 4 deletions

View File

@ -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(

View File

@ -66,3 +66,11 @@ fn issue7730(a: u8) {
// comment after `panic!`
assert!(!(a > 2), "panic with comment");
}
fn issue12505() {
struct Foo<T, const N: usize>(T);
impl<T, const N: usize> Foo<T, N> {
const BAR: () = assert!(!(N == 0), );
}
}

View File

@ -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

View File

@ -66,3 +66,11 @@ fn issue7730(a: u8) {
// comment after `panic!`
assert!(!(a > 2), "panic with comment");
}
fn issue12505() {
struct Foo<T, const N: usize>(T);
impl<T, const N: usize> Foo<T, N> {
const BAR: () = assert!(!(N == 0), );
}
}

View File

@ -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

View File

@ -83,3 +83,13 @@ fn issue7730(a: u8) {
panic!("panic with comment") // comment after `panic!`
}
}
fn issue12505() {
struct Foo<T, const N: usize>(T);
impl<T, const N: usize> Foo<T, N> {
const BAR: () = if N == 0 {
panic!()
};
}
}