Skip to content

Commit

Permalink
Auto merge of #12805 - Alexendoo:blocks-in-conditions-closures, r=dswij
Browse files Browse the repository at this point in the history
Don't lint blocks in closures for blocks_in_conditions

Seemed like an outlier for the lint which generally caught only the syntactically confusing cases, it lints blocks in closures but excludes closures passed to iterator methods, this changes it to ignore closures in general

changelog: none
  • Loading branch information
bors committed Jun 8, 2024
2 parents 0ea88b9 + b21ee38 commit 48686ad
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 158 deletions.
32 changes: 2 additions & 30 deletions clippy_lints/src/blocks_in_conditions.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_block_with_applicability;
use clippy_utils::ty::implements_trait;
use clippy_utils::visitors::{for_each_expr_without_closures, Descend};
use clippy_utils::{get_parent_expr, higher, is_from_proc_macro};
use core::ops::ControlFlow;
use clippy_utils::{higher, is_from_proc_macro};
use rustc_errors::Applicability;
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::declare_lint_pass;
use rustc_span::sym;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -124,30 +120,6 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
);
}
}
} else {
let _: Option<!> = for_each_expr_without_closures(cond, |e| {
if let ExprKind::Closure(closure) = e.kind {
// do not lint if the closure is called using an iterator (see #1141)
if let Some(parent) = get_parent_expr(cx, e)
&& let ExprKind::MethodCall(_, self_arg, _, _) = &parent.kind
&& let caller = cx.typeck_results().expr_ty(self_arg)
&& let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator)
&& implements_trait(cx, caller, iter_id, &[])
{
return ControlFlow::Continue(Descend::No);
}

let body = cx.tcx.hir().body(closure.body);
let ex = &body.value;
if let ExprKind::Block(block, _) = ex.kind {
if !body.value.span.from_expansion() && !block.stmts.is_empty() {
span_lint(cx, BLOCKS_IN_CONDITIONS, ex.span, complex_block_message.clone());
return ControlFlow::Continue(Descend::No);
}
}
}
ControlFlow::Continue(Descend::Yes)
});
}
}
}
13 changes: 13 additions & 0 deletions tests/ui/blocks_in_conditions.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,17 @@ mod issue_12016 {
}
}

fn in_closure() {
let v = vec![1, 2, 3];
if v.into_iter()
.filter(|x| {
let y = x + 1;
y > 3
})
.any(|x| x == 5)
{
println!("contains 4!");
}
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/blocks_in_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,17 @@ mod issue_12016 {
}
}

fn in_closure() {
let v = vec![1, 2, 3];
if v.into_iter()
.filter(|x| {
let y = x + 1;
y > 3
})
.any(|x| x == 5)
{
println!("contains 4!");
}
}

fn main() {}
89 changes: 0 additions & 89 deletions tests/ui/blocks_in_conditions_closure.rs

This file was deleted.

39 changes: 0 additions & 39 deletions tests/ui/blocks_in_conditions_closure.stderr

This file was deleted.

0 comments on commit 48686ad

Please sign in to comment.