Skip to content
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
14 changes: 14 additions & 0 deletions clippy_lints/src/semicolon_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::SpanRangeExt;
use rustc_errors::Applicability;
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
Expand Down Expand Up @@ -82,6 +83,19 @@ impl SemicolonBlock {
let insert_span = tail.span.source_callsite().shrink_to_hi();
let remove_span = semi_span.with_lo(block.span.hi());

// If the block is surrounded by parens (`({ 0 });`), the author probably knows what
// they're doing and why, so don't get in their way.
//
// This has the additional benefit of stopping the block being parsed as a function call:
// ```
// fn foo() {
// ({ 0 }); // if we remove this `;`, this will parse as a `({ 0 })(5);` function call
// (5);
// }
if remove_span.check_source_text(cx, |src| src.contains(')')) {
return;
}

if self.semicolon_inside_block_ignore_singleline && get_line(cx, remove_span) == get_line(cx, insert_span) {
return;
}
Expand Down
17 changes: 16 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 @@ -87,6 +88,20 @@ fn main() {
unit_fn_block()
}

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

({
0;
0
});

(({ 0 })) ;

( ( { 0 } ) ) ;
}

pub fn issue15388() {
#[rustfmt::skip]
{0; 0};
Expand Down
17 changes: 16 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 @@ -87,6 +88,20 @@ fn main() {
unit_fn_block()
}

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

({
0;
0
});

(({ 0 })) ;

( ( { 0 } ) ) ;
}

pub fn issue15388() {
#[rustfmt::skip]
{0; 0};
Expand Down
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