Skip to content

manual_assert: do not add extra semicolon #12536

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 2 commits into from
Mar 25, 2024
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
5 changes: 3 additions & 2 deletions clippy_lints/src/manual_assert.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -63,7 +63,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
_ => (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(
Expand Down
12 changes: 3 additions & 9 deletions clippy_lints/src/needless_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::{
higher, is_else_clause, is_expn_of, peel_blocks, peel_blocks_with_stmt, span_extract_comment, SpanlessEq,
higher, is_else_clause, is_expn_of, is_parent_stmt, peel_blocks, peel_blocks_with_stmt, span_extract_comment,
SpanlessEq,
};
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, Node, UnOp};
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::source_map::Spanned;
Expand Down Expand Up @@ -135,13 +136,6 @@ fn condition_needs_parentheses(e: &Expr<'_>) -> bool {
false
}

fn is_parent_stmt(cx: &LateContext<'_>, id: HirId) -> bool {
matches!(
cx.tcx.parent_hir_node(id),
Node::Stmt(..) | Node::Block(Block { stmts: &[], .. })
)
}

impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
use self::Expression::{Bool, RetBool};
Expand Down
9 changes: 9 additions & 0 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3335,3 +3335,12 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
repeat(String::from("super")).take(go_up_by).chain(path).join("::")
}
}

/// Returns true if the specified `HirId` is the top-level expression of a statement or the only
/// expression in a block.
pub fn is_parent_stmt(cx: &LateContext<'_>, id: HirId) -> bool {
matches!(
cx.tcx.parent_hir_node(id),
Node::Stmt(..) | Node::Block(Block { stmts: &[], .. })
)
}
8 changes: 8 additions & 0 deletions tests/ui/manual_assert.edition2018.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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), );
}
}
11 changes: 10 additions & 1 deletion tests/ui/manual_assert.edition2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

8 changes: 8 additions & 0 deletions tests/ui/manual_assert.edition2021.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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), );
}
}
11 changes: 10 additions & 1 deletion tests/ui/manual_assert.edition2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

10 changes: 10 additions & 0 deletions tests/ui/manual_assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
};
}
}