diff options
| author | lapla-cogito <me@lapla.dev> | 2025-03-02 22:24:43 +0900 |
|---|---|---|
| committer | lapla-cogito <me@lapla.dev> | 2025-03-08 16:57:06 +0900 |
| commit | 243e0ee2741d78e1fc73a045c8cc0319fa98b259 (patch) | |
| tree | b8d2edb353b14dc2ac89ba17a986f5a9c23ada45 | |
| parent | 1419ac2982e8c33496f896cc98c4bb4b14c94813 (diff) | |
| download | rust-243e0ee2741d78e1fc73a045c8cc0319fa98b259.tar.gz rust-243e0ee2741d78e1fc73a045c8cc0319fa98b259.zip | |
don't trigger `blocks_in_conditions` when the condition contains a `return`
| -rw-r--r-- | clippy_lints/src/blocks_in_conditions.rs | 9 | ||||
| -rw-r--r-- | tests/ui/blocks_in_conditions.fixed | 7 | ||||
| -rw-r--r-- | tests/ui/blocks_in_conditions.rs | 7 |
3 files changed, 22 insertions, 1 deletions
diff --git a/clippy_lints/src/blocks_in_conditions.rs b/clippy_lints/src/blocks_in_conditions.rs index aab0af0d743..011962846cb 100644 --- a/clippy_lints/src/blocks_in_conditions.rs +++ b/clippy_lints/src/blocks_in_conditions.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_block_with_applicability; -use clippy_utils::{higher, is_from_proc_macro}; +use clippy_utils::{contains_return, higher, is_from_proc_macro}; use rustc_errors::Applicability; use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource}; use rustc_lint::{LateContext, LateLintPass, LintContext}; @@ -86,6 +86,13 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions { if expr.span.from_expansion() || ex.span.from_expansion() { return; } + + // Linting should not be triggered to cases where `return` is included in the condition. + // #9911 + if contains_return(block.expr) { + return; + } + let mut applicability = Applicability::MachineApplicable; span_lint_and_sugg( cx, diff --git a/tests/ui/blocks_in_conditions.fixed b/tests/ui/blocks_in_conditions.fixed index df375e37057..cd307e803d0 100644 --- a/tests/ui/blocks_in_conditions.fixed +++ b/tests/ui/blocks_in_conditions.fixed @@ -118,6 +118,13 @@ mod issue_12016 { } } +fn issue_9911() { + if { return } {} + + let a = 1; + if { if a == 1 { return } else { true } } {} +} + fn in_closure() { let v = vec![1, 2, 3]; if v.into_iter() diff --git a/tests/ui/blocks_in_conditions.rs b/tests/ui/blocks_in_conditions.rs index 1d9c9dd4246..6a211c8edfd 100644 --- a/tests/ui/blocks_in_conditions.rs +++ b/tests/ui/blocks_in_conditions.rs @@ -118,6 +118,13 @@ mod issue_12016 { } } +fn issue_9911() { + if { return } {} + + let a = 1; + if { if a == 1 { return } else { true } } {} +} + fn in_closure() { let v = vec![1, 2, 3]; if v.into_iter() |
