Skip to content

Commit 49e2f37

Browse files
fix(semicolon_inside_block): don't lint if block is in parens (#15626)
Decided not to lint in this case after all, as per #15391 (comment) Supersedes #15421, fixes #15380, fixes #15389 changelog: [`semicolon_inside_block`]: don't lint if block is in parens
2 parents 368b235 + 36c2c0a commit 49e2f37

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

clippy_lints/src/semicolon_block.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
3+
use clippy_utils::source::SpanRangeExt;
34
use rustc_errors::Applicability;
45
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
56
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -82,6 +83,19 @@ impl SemicolonBlock {
8283
let insert_span = tail.span.source_callsite().shrink_to_hi();
8384
let remove_span = semi_span.with_lo(block.span.hi());
8485

86+
// If the block is surrounded by parens (`({ 0 });`), the author probably knows what
87+
// they're doing and why, so don't get in their way.
88+
//
89+
// This has the additional benefit of stopping the block being parsed as a function call:
90+
// ```
91+
// fn foo() {
92+
// ({ 0 }); // if we remove this `;`, this will parse as a `({ 0 })(5);` function call
93+
// (5);
94+
// }
95+
if remove_span.check_source_text(cx, |src| src.contains(')')) {
96+
return;
97+
}
98+
8599
if self.semicolon_inside_block_ignore_singleline && get_line(cx, remove_span) == get_line(cx, insert_span) {
86100
return;
87101
}

tests/ui/semicolon_inside_block.fixed

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
clippy::unused_unit,
44
clippy::unnecessary_operation,
55
clippy::no_effect,
6-
clippy::single_element_loop
6+
clippy::single_element_loop,
7+
clippy::double_parens
78
)]
89
#![warn(clippy::semicolon_inside_block)]
910

@@ -87,6 +88,20 @@ fn main() {
8788
unit_fn_block()
8889
}
8990

91+
#[rustfmt::skip]
92+
fn issue15380() {
93+
( {0;0});
94+
95+
({
96+
0;
97+
0
98+
});
99+
100+
(({ 0 })) ;
101+
102+
( ( { 0 } ) ) ;
103+
}
104+
90105
pub fn issue15388() {
91106
#[rustfmt::skip]
92107
{0; 0};

tests/ui/semicolon_inside_block.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
clippy::unused_unit,
44
clippy::unnecessary_operation,
55
clippy::no_effect,
6-
clippy::single_element_loop
6+
clippy::single_element_loop,
7+
clippy::double_parens
78
)]
89
#![warn(clippy::semicolon_inside_block)]
910

@@ -87,6 +88,20 @@ fn main() {
8788
unit_fn_block()
8889
}
8990

91+
#[rustfmt::skip]
92+
fn issue15380() {
93+
( {0;0});
94+
95+
({
96+
0;
97+
0
98+
});
99+
100+
(({ 0 })) ;
101+
102+
( ( { 0 } ) ) ;
103+
}
104+
90105
pub fn issue15388() {
91106
#[rustfmt::skip]
92107
{0; 0};

tests/ui/semicolon_inside_block.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: consider moving the `;` inside the block for consistent formatting
2-
--> tests/ui/semicolon_inside_block.rs:38:5
2+
--> tests/ui/semicolon_inside_block.rs:39:5
33
|
44
LL | { unit_fn_block() };
55
| ^^^^^^^^^^^^^^^^^^^^
@@ -13,7 +13,7 @@ LL + { unit_fn_block(); }
1313
|
1414

1515
error: consider moving the `;` inside the block for consistent formatting
16-
--> tests/ui/semicolon_inside_block.rs:40:5
16+
--> tests/ui/semicolon_inside_block.rs:41:5
1717
|
1818
LL | unsafe { unit_fn_block() };
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -25,7 +25,7 @@ LL + unsafe { unit_fn_block(); }
2525
|
2626

2727
error: consider moving the `;` inside the block for consistent formatting
28-
--> tests/ui/semicolon_inside_block.rs:49:5
28+
--> tests/ui/semicolon_inside_block.rs:50:5
2929
|
3030
LL | / {
3131
LL | |
@@ -41,7 +41,7 @@ LL ~ }
4141
|
4242

4343
error: consider moving the `;` inside the block for consistent formatting
44-
--> tests/ui/semicolon_inside_block.rs:63:5
44+
--> tests/ui/semicolon_inside_block.rs:64:5
4545
|
4646
LL | { m!(()) };
4747
| ^^^^^^^^^^^

0 commit comments

Comments
 (0)