Skip to content

Commit 1b62885

Browse files
authored
don't trigger blocks_in_conditions when the condition contains a return (#14338)
fixes #9911 changelog: [`blocks_in_conditions`]: don't trigger the lint when the condition contains a `return`
2 parents f83c94c + 243e0ee commit 1b62885

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Diff for: clippy_lints/src/blocks_in_conditions.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_block_with_applicability;
3-
use clippy_utils::{higher, is_from_proc_macro};
3+
use clippy_utils::{contains_return, higher, is_from_proc_macro};
44
use rustc_errors::Applicability;
55
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
66
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -86,6 +86,13 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
8686
if expr.span.from_expansion() || ex.span.from_expansion() {
8787
return;
8888
}
89+
90+
// Linting should not be triggered to cases where `return` is included in the condition.
91+
// #9911
92+
if contains_return(block.expr) {
93+
return;
94+
}
95+
8996
let mut applicability = Applicability::MachineApplicable;
9097
span_lint_and_sugg(
9198
cx,

Diff for: tests/ui/blocks_in_conditions.fixed

+7
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ mod issue_12016 {
118118
}
119119
}
120120

121+
fn issue_9911() {
122+
if { return } {}
123+
124+
let a = 1;
125+
if { if a == 1 { return } else { true } } {}
126+
}
127+
121128
fn in_closure() {
122129
let v = vec![1, 2, 3];
123130
if v.into_iter()

Diff for: tests/ui/blocks_in_conditions.rs

+7
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ mod issue_12016 {
118118
}
119119
}
120120

121+
fn issue_9911() {
122+
if { return } {}
123+
124+
let a = 1;
125+
if { if a == 1 { return } else { true } } {}
126+
}
127+
121128
fn in_closure() {
122129
let v = vec![1, 2, 3];
123130
if v.into_iter()

0 commit comments

Comments
 (0)