From a5fa3995d6421c43e8b9a2ada45cc7301f9d495c Mon Sep 17 00:00:00 2001 From: Taym Haddadi Date: Mon, 9 Mar 2026 02:08:04 +0100 Subject: [PATCH] Fix semicolon-inside-block inside try_blocks Signed-off-by: Taym Haddadi --- clippy_lints/src/semicolon_block.rs | 2 +- tests/ui/semicolon_inside_block.fixed | 9 +++++++++ tests/ui/semicolon_inside_block.rs | 9 +++++++++ tests/ui/semicolon_inside_block.stderr | 8 ++++---- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/semicolon_block.rs b/clippy_lints/src/semicolon_block.rs index 7c68762b22c0..e806123596b8 100644 --- a/clippy_lints/src/semicolon_block.rs +++ b/clippy_lints/src/semicolon_block.rs @@ -170,7 +170,7 @@ impl LateLintPass<'_> for SemicolonBlock { StmtKind::Semi(Expr { kind: ExprKind::Block(block, _), .. - }) if !block.span.from_expansion() => { + }) if !block.span.from_expansion() && !block.targeted_by_break => { let attrs = cx.tcx.hir_attrs(stmt.hir_id); if !attrs.is_empty() && !cx.tcx.features().stmt_expr_attributes() { return; diff --git a/tests/ui/semicolon_inside_block.fixed b/tests/ui/semicolon_inside_block.fixed index 468f0a5b1e47..c7174881a4fa 100644 --- a/tests/ui/semicolon_inside_block.fixed +++ b/tests/ui/semicolon_inside_block.fixed @@ -7,6 +7,7 @@ clippy::double_parens )] #![warn(clippy::semicolon_inside_block)] +#![feature(try_blocks)] macro_rules! m { (()) => { @@ -106,3 +107,11 @@ pub fn issue15388() { #[rustfmt::skip] {0; 0}; } + +fn issue_try_blocks() { + // try blocks should NOT trigger semicolon_inside_block: + // moving `;` inside changes the return type (e.g. `Option` -> `Option<()>`) + // which in turn changes the type constraints on `?` operators inside the block, + // causing type errors. + try { Some(1)? }; +} diff --git a/tests/ui/semicolon_inside_block.rs b/tests/ui/semicolon_inside_block.rs index 101374af2647..25be9bee4f53 100644 --- a/tests/ui/semicolon_inside_block.rs +++ b/tests/ui/semicolon_inside_block.rs @@ -7,6 +7,7 @@ clippy::double_parens )] #![warn(clippy::semicolon_inside_block)] +#![feature(try_blocks)] macro_rules! m { (()) => { @@ -106,3 +107,11 @@ pub fn issue15388() { #[rustfmt::skip] {0; 0}; } + +fn issue_try_blocks() { + // try blocks should NOT trigger semicolon_inside_block: + // moving `;` inside changes the return type (e.g. `Option` -> `Option<()>`) + // which in turn changes the type constraints on `?` operators inside the block, + // causing type errors. + try { Some(1)? }; +} diff --git a/tests/ui/semicolon_inside_block.stderr b/tests/ui/semicolon_inside_block.stderr index 2046dd1c36be..e16590374295 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:39:5 + --> tests/ui/semicolon_inside_block.rs:40: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:41:5 + --> tests/ui/semicolon_inside_block.rs:42: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:50:5 + --> tests/ui/semicolon_inside_block.rs:51:5 | LL | / { LL | | @@ -41,7 +41,7 @@ LL ~ } | error: consider moving the `;` inside the block for consistent formatting - --> tests/ui/semicolon_inside_block.rs:64:5 + --> tests/ui/semicolon_inside_block.rs:65:5 | LL | { m!(()) }; | ^^^^^^^^^^^