diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 844bc1b0e390..2d1fad40d9f9 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -732,7 +732,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co store.register_late_pass(|_| Box::new(from_raw_with_void_ptr::FromRawWithVoidPtr)); store.register_late_pass(|_| Box::new(suspicious_xor_used_as_pow::ConfusingXorAndPow)); store.register_late_pass(move |_| Box::new(manual_is_ascii_check::ManualIsAsciiCheck::new(conf))); - store.register_late_pass(move |_| Box::new(semicolon_block::SemicolonBlock::new(conf))); + store.register_early_pass(move || Box::new(semicolon_block::SemicolonBlock::new(conf))); store.register_late_pass(|_| Box::new(permissions_set_readonly_false::PermissionsSetReadonlyFalse)); store.register_late_pass(|_| Box::new(size_of_ref::SizeOfRef)); store.register_late_pass(|_| Box::new(multiple_unsafe_ops_per_block::MultipleUnsafeOpsPerBlock)); diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index db91c57b1816..4105f32887c6 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -1,8 +1,8 @@ use clippy_config::Conf; use clippy_utils::diagnostics::span_lint_and_then; +use rustc_ast::{Block, Expr, ExprKind, Stmt, StmtKind}; use rustc_errors::Applicability; -use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind}; -use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_session::impl_lint_pass; use rustc_span::Span; @@ -78,7 +78,7 @@ impl SemicolonBlock { } } - fn semicolon_inside_block(&self, cx: &LateContext<'_>, block: &Block<'_>, tail: &Expr<'_>, semi_span: Span) { + fn semicolon_inside_block(&self, cx: &impl LintContext, block: &Block, tail: &Expr, semi_span: Span) { let insert_span = tail.span.source_callsite().shrink_to_hi(); let remove_span = semi_span.with_lo(block.span.hi()); @@ -101,7 +101,7 @@ impl SemicolonBlock { ); } - fn semicolon_outside_block(&self, cx: &LateContext<'_>, block: &Block<'_>, tail_stmt_expr: &Expr<'_>) { + fn semicolon_outside_block(&self, cx: &impl LintContext, block: &Block, tail_stmt_expr: &Expr) { let insert_span = block.span.shrink_to_hi(); // For macro call semicolon statements (`mac!();`), the statement's span does not actually @@ -137,25 +137,27 @@ impl SemicolonBlock { } } -impl LateLintPass<'_> for SemicolonBlock { - fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { - match stmt.kind { - StmtKind::Expr(Expr { - kind: ExprKind::Block(block, _), - .. - }) if !block.span.from_expansion() && stmt.span.contains(block.span) => { - if block.expr.is_none() - && let [.., stmt] = block.stmts - && let StmtKind::Semi(expr) = stmt.kind +impl EarlyLintPass for SemicolonBlock { + fn check_stmt(&mut self, cx: &EarlyContext<'_>, stmt: &Stmt) { + match &stmt.kind { + StmtKind::Expr(expr) + if let ExprKind::Block(block, _) = &expr.kind + && !block.span.from_expansion() + && stmt.span.contains(block.span) => + { + if let Some(stmt) = block.stmts.last() + && let StmtKind::Semi(expr) = &stmt.kind { self.semicolon_outside_block(cx, block, expr); } }, - StmtKind::Semi(Expr { - kind: ExprKind::Block(block, _), - .. - }) if !block.span.from_expansion() => { - if let Some(tail) = block.expr { + StmtKind::Semi(expr) + if let ExprKind::Block(block, _) = &expr.kind + && !block.span.from_expansion() => + { + if let Some(expr) = block.stmts.last() + && let StmtKind::Expr(tail) = &expr.kind + { self.semicolon_inside_block(cx, block, tail, stmt.span); } }, @@ -164,6 +166,6 @@ impl LateLintPass<'_> for SemicolonBlock { } } -fn get_line(cx: &LateContext<'_>, span: Span) -> Option { +fn get_line(cx: &impl LintContext, span: Span) -> Option { cx.sess().source_map().lookup_line(span.lo()).ok().map(|line| line.line) } diff --git a/tests/ui/semicolon_inside_block.fixed b/tests/ui/semicolon_inside_block.fixed index 7eb53e733ad5..a96c349b656b 100644 --- a/tests/ui/semicolon_inside_block.fixed +++ b/tests/ui/semicolon_inside_block.fixed @@ -3,7 +3,8 @@ clippy::unused_unit, clippy::unnecessary_operation, clippy::no_effect, - clippy::single_element_loop + clippy::single_element_loop, + clippy::double_parens )] #![warn(clippy::semicolon_inside_block)] @@ -86,3 +87,16 @@ fn main() { unit_fn_block() } + +fn issue15380() { + #[rustfmt::skip] + ( {0;0}); + + ({ + 0; + 0 + }); + + #[rustfmt::skip] + (({0})) ; +} diff --git a/tests/ui/semicolon_inside_block.rs b/tests/ui/semicolon_inside_block.rs index 9fa5b117194d..700a2454a48d 100644 --- a/tests/ui/semicolon_inside_block.rs +++ b/tests/ui/semicolon_inside_block.rs @@ -3,7 +3,8 @@ clippy::unused_unit, clippy::unnecessary_operation, clippy::no_effect, - clippy::single_element_loop + clippy::single_element_loop, + clippy::double_parens )] #![warn(clippy::semicolon_inside_block)] @@ -86,3 +87,16 @@ fn main() { unit_fn_block() } + +fn issue15380() { + #[rustfmt::skip] + ( {0;0}); + + ({ + 0; + 0 + }); + + #[rustfmt::skip] + (({0})) ; +} diff --git a/tests/ui/semicolon_inside_block.stderr b/tests/ui/semicolon_inside_block.stderr index 23433f4e7ef9..2046dd1c36be 100644 --- a/tests/ui/semicolon_inside_block.stderr +++ b/tests/ui/semicolon_inside_block.stderr @@ -1,5 +1,5 @@ error: consider moving the `;` inside the block for consistent formatting - --> tests/ui/semicolon_inside_block.rs:38:5 + --> tests/ui/semicolon_inside_block.rs:39:5 | LL | { unit_fn_block() }; | ^^^^^^^^^^^^^^^^^^^^ @@ -13,7 +13,7 @@ LL + { unit_fn_block(); } | error: consider moving the `;` inside the block for consistent formatting - --> tests/ui/semicolon_inside_block.rs:40:5 + --> tests/ui/semicolon_inside_block.rs:41:5 | LL | unsafe { unit_fn_block() }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -25,7 +25,7 @@ LL + unsafe { unit_fn_block(); } | error: consider moving the `;` inside the block for consistent formatting - --> tests/ui/semicolon_inside_block.rs:49:5 + --> tests/ui/semicolon_inside_block.rs:50:5 | LL | / { LL | | @@ -41,7 +41,7 @@ LL ~ } | error: consider moving the `;` inside the block for consistent formatting - --> tests/ui/semicolon_inside_block.rs:63:5 + --> tests/ui/semicolon_inside_block.rs:64:5 | LL | { m!(()) }; | ^^^^^^^^^^^ diff --git a/tests/ui/semicolon_outside_block.fixed b/tests/ui/semicolon_outside_block.fixed index a3be80b49284..4ddf91c5c541 100644 --- a/tests/ui/semicolon_outside_block.fixed +++ b/tests/ui/semicolon_outside_block.fixed @@ -121,3 +121,16 @@ fn issue14926() { }, } } + +fn f() { + struct Vec; + impl Vec { + unsafe fn set_len(&mut self, _len: usize) {} + fn clear(&mut self) { + unsafe { + //~^ semicolon_outside_block + self.set_len(0) + }; + } + } +} diff --git a/tests/ui/semicolon_outside_block.rs b/tests/ui/semicolon_outside_block.rs index 3b7bf68029f3..f414a354a153 100644 --- a/tests/ui/semicolon_outside_block.rs +++ b/tests/ui/semicolon_outside_block.rs @@ -121,3 +121,16 @@ fn issue14926() { }, } } + +fn f() { + struct Vec; + impl Vec { + unsafe fn set_len(&mut self, _len: usize) {} + fn clear(&mut self) { + unsafe { + //~^ semicolon_outside_block + self.set_len(0); + } + } + } +} diff --git a/tests/ui/semicolon_outside_block.stderr b/tests/ui/semicolon_outside_block.stderr index 18d6dc697f2e..bfa43b942d7c 100644 --- a/tests/ui/semicolon_outside_block.stderr +++ b/tests/ui/semicolon_outside_block.stderr @@ -82,5 +82,20 @@ LL ~ line!() LL ~ }; | -error: aborting due to 6 previous errors +error: consider moving the `;` outside the block for consistent formatting + --> tests/ui/semicolon_outside_block.rs:130:13 + | +LL | / unsafe { +LL | | +LL | | self.set_len(0); +LL | | } + | |_____________^ + | +help: put the `;` here + | +LL ~ self.set_len(0) +LL ~ }; + | + +error: aborting due to 7 previous errors