Skip to content

Commit b1b7352

Browse files
committed
Auto merge of #12536 - samueltardieu:issue-12505, r=Manishearth
`manual_assert`: do not add extra semicolon Fixes #12505 changelog: [`manual_assert`]: do not add extra semicolon to suggestion
2 parents 4ef57d3 + c137c78 commit b1b7352

8 files changed

+61
-13
lines changed

clippy_lints/src/manual_assert.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::rustc_lint::LintContext;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::macros::root_macro_call;
4-
use clippy_utils::{is_else_clause, peel_blocks_with_stmt, span_extract_comment, sugg};
4+
use clippy_utils::{is_else_clause, is_parent_stmt, peel_blocks_with_stmt, span_extract_comment, sugg};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, UnOp};
77
use rustc_lint::{LateContext, LateLintPass};
@@ -63,7 +63,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualAssert {
6363
_ => (cond, "!"),
6464
};
6565
let cond_sugg = sugg::Sugg::hir_with_applicability(cx, cond, "..", &mut applicability).maybe_par();
66-
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip});");
66+
let semicolon = if is_parent_stmt(cx, expr.hir_id) { ";" } else { "" };
67+
let sugg = format!("assert!({not}{cond_sugg}, {format_args_snip}){semicolon}");
6768
// we show to the user the suggestion without the comments, but when applying the fix, include the
6869
// comments in the block
6970
span_lint_and_then(

clippy_lints/src/needless_bool.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
66
use clippy_utils::source::snippet_with_applicability;
77
use clippy_utils::sugg::Sugg;
88
use clippy_utils::{
9-
higher, is_else_clause, is_expn_of, peel_blocks, peel_blocks_with_stmt, span_extract_comment, SpanlessEq,
9+
higher, is_else_clause, is_expn_of, is_parent_stmt, peel_blocks, peel_blocks_with_stmt, span_extract_comment,
10+
SpanlessEq,
1011
};
1112
use rustc_ast::ast::LitKind;
1213
use rustc_errors::Applicability;
13-
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, Node, UnOp};
14+
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
1415
use rustc_lint::{LateContext, LateLintPass};
1516
use rustc_session::declare_lint_pass;
1617
use rustc_span::source_map::Spanned;
@@ -135,13 +136,6 @@ fn condition_needs_parentheses(e: &Expr<'_>) -> bool {
135136
false
136137
}
137138

138-
fn is_parent_stmt(cx: &LateContext<'_>, id: HirId) -> bool {
139-
matches!(
140-
cx.tcx.parent_hir_node(id),
141-
Node::Stmt(..) | Node::Block(Block { stmts: &[], .. })
142-
)
143-
}
144-
145139
impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
146140
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
147141
use self::Expression::{Bool, RetBool};

clippy_utils/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,3 +3335,12 @@ fn maybe_get_relative_path(from: &DefPath, to: &DefPath, max_super: usize) -> St
33353335
repeat(String::from("super")).take(go_up_by).chain(path).join("::")
33363336
}
33373337
}
3338+
3339+
/// Returns true if the specified `HirId` is the top-level expression of a statement or the only
3340+
/// expression in a block.
3341+
pub fn is_parent_stmt(cx: &LateContext<'_>, id: HirId) -> bool {
3342+
matches!(
3343+
cx.tcx.parent_hir_node(id),
3344+
Node::Stmt(..) | Node::Block(Block { stmts: &[], .. })
3345+
)
3346+
}

tests/ui/manual_assert.edition2018.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ fn issue7730(a: u8) {
6666
// comment after `panic!`
6767
assert!(!(a > 2), "panic with comment");
6868
}
69+
70+
fn issue12505() {
71+
struct Foo<T, const N: usize>(T);
72+
73+
impl<T, const N: usize> Foo<T, N> {
74+
const BAR: () = assert!(!(N == 0), );
75+
}
76+
}

tests/ui/manual_assert.edition2018.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,14 @@ help: try instead
8282
LL | assert!(!(a > 2), "panic with comment");
8383
|
8484

85-
error: aborting due to 9 previous errors
85+
error: only a `panic!` in `if`-then statement
86+
--> tests/ui/manual_assert.rs:91:25
87+
|
88+
LL | const BAR: () = if N == 0 {
89+
| _________________________^
90+
LL | | panic!()
91+
LL | | };
92+
| |_________^ help: try instead: `assert!(!(N == 0), )`
93+
94+
error: aborting due to 10 previous errors
8695

tests/ui/manual_assert.edition2021.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ fn issue7730(a: u8) {
6666
// comment after `panic!`
6767
assert!(!(a > 2), "panic with comment");
6868
}
69+
70+
fn issue12505() {
71+
struct Foo<T, const N: usize>(T);
72+
73+
impl<T, const N: usize> Foo<T, N> {
74+
const BAR: () = assert!(!(N == 0), );
75+
}
76+
}

tests/ui/manual_assert.edition2021.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,14 @@ help: try instead
8282
LL | assert!(!(a > 2), "panic with comment");
8383
|
8484

85-
error: aborting due to 9 previous errors
85+
error: only a `panic!` in `if`-then statement
86+
--> tests/ui/manual_assert.rs:91:25
87+
|
88+
LL | const BAR: () = if N == 0 {
89+
| _________________________^
90+
LL | | panic!()
91+
LL | | };
92+
| |_________^ help: try instead: `assert!(!(N == 0), )`
93+
94+
error: aborting due to 10 previous errors
8695

tests/ui/manual_assert.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,13 @@ fn issue7730(a: u8) {
8383
panic!("panic with comment") // comment after `panic!`
8484
}
8585
}
86+
87+
fn issue12505() {
88+
struct Foo<T, const N: usize>(T);
89+
90+
impl<T, const N: usize> Foo<T, N> {
91+
const BAR: () = if N == 0 {
92+
panic!()
93+
};
94+
}
95+
}

0 commit comments

Comments
 (0)