Skip to content

fix: semicolon_inside_block don't fire if block is surrounded by parens #15421

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
42 changes: 22 additions & 20 deletions clippy_lints/src/semicolon_block.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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());

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
},
Expand All @@ -164,6 +166,6 @@ impl LateLintPass<'_> for SemicolonBlock {
}
}

fn get_line(cx: &LateContext<'_>, span: Span) -> Option<usize> {
fn get_line(cx: &impl LintContext, span: Span) -> Option<usize> {
cx.sess().source_map().lookup_line(span.lo()).ok().map(|line| line.line)
}
16 changes: 15 additions & 1 deletion tests/ui/semicolon_inside_block.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down Expand Up @@ -86,3 +87,16 @@ fn main() {

unit_fn_block()
}

fn issue15380() {
#[rustfmt::skip]
( {0;0});

({
0;
0
});

#[rustfmt::skip]
(({0})) ;
}
16 changes: 15 additions & 1 deletion tests/ui/semicolon_inside_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down Expand Up @@ -86,3 +87,16 @@ fn main() {

unit_fn_block()
}

fn issue15380() {
#[rustfmt::skip]
( {0;0});

({
0;
0
});

#[rustfmt::skip]
(({0})) ;
}
8 changes: 4 additions & 4 deletions tests/ui/semicolon_inside_block.stderr
Original file line number Diff line number Diff line change
@@ -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() };
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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 | |
Expand All @@ -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!(()) };
| ^^^^^^^^^^^
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/semicolon_outside_block.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}
}
}
13 changes: 13 additions & 0 deletions tests/ui/semicolon_outside_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
17 changes: 16 additions & 1 deletion tests/ui/semicolon_outside_block.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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