Skip to content

Commit 5e1d741

Browse files
authored
Fix semicolon-inside-block inside try_blocks (rust-lang#16697)
fixes rust-lang#16696 changelog: [`semicolon_inside_block`]: fix false positive in `try` blocks where moving `;` inside changes the block's return type and causes type errors.
2 parents a721c29 + a5fa399 commit 5e1d741

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

clippy_lints/src/semicolon_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl LateLintPass<'_> for SemicolonBlock {
170170
StmtKind::Semi(Expr {
171171
kind: ExprKind::Block(block, _),
172172
..
173-
}) if !block.span.from_expansion() => {
173+
}) if !block.span.from_expansion() && !block.targeted_by_break => {
174174
let attrs = cx.tcx.hir_attrs(stmt.hir_id);
175175
if !attrs.is_empty() && !cx.tcx.features().stmt_expr_attributes() {
176176
return;

tests/ui/semicolon_inside_block.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
clippy::double_parens
88
)]
99
#![warn(clippy::semicolon_inside_block)]
10+
#![feature(try_blocks)]
1011

1112
macro_rules! m {
1213
(()) => {
@@ -106,3 +107,11 @@ pub fn issue15388() {
106107
#[rustfmt::skip]
107108
{0; 0};
108109
}
110+
111+
fn issue_try_blocks() {
112+
// try blocks should NOT trigger semicolon_inside_block:
113+
// moving `;` inside changes the return type (e.g. `Option<i32>` -> `Option<()>`)
114+
// which in turn changes the type constraints on `?` operators inside the block,
115+
// causing type errors.
116+
try { Some(1)? };
117+
}

tests/ui/semicolon_inside_block.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
clippy::double_parens
88
)]
99
#![warn(clippy::semicolon_inside_block)]
10+
#![feature(try_blocks)]
1011

1112
macro_rules! m {
1213
(()) => {
@@ -106,3 +107,11 @@ pub fn issue15388() {
106107
#[rustfmt::skip]
107108
{0; 0};
108109
}
110+
111+
fn issue_try_blocks() {
112+
// try blocks should NOT trigger semicolon_inside_block:
113+
// moving `;` inside changes the return type (e.g. `Option<i32>` -> `Option<()>`)
114+
// which in turn changes the type constraints on `?` operators inside the block,
115+
// causing type errors.
116+
try { Some(1)? };
117+
}

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:39:5
2+
--> tests/ui/semicolon_inside_block.rs:40: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:41:5
16+
--> tests/ui/semicolon_inside_block.rs:42: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:50:5
28+
--> tests/ui/semicolon_inside_block.rs:51: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:64:5
44+
--> tests/ui/semicolon_inside_block.rs:65:5
4545
|
4646
LL | { m!(()) };
4747
| ^^^^^^^^^^^

0 commit comments

Comments
 (0)