don't trigger blocks_in_conditions when the condition contains a return

This commit is contained in:
lapla-cogito 2025-03-02 22:24:43 +09:00
parent 1419ac2982
commit 243e0ee274
No known key found for this signature in database
GPG key ID: B39C71D9F127FF9F
3 changed files with 22 additions and 1 deletions

View file

@ -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,

View file

@ -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()

View file

@ -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()