Skip to content

Commit 48686ad

Browse files
committed
Auto merge of #12805 - Alexendoo:blocks-in-conditions-closures, r=dswij
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
2 parents 0ea88b9 + b21ee38 commit 48686ad

5 files changed

+28
-158
lines changed

clippy_lints/src/blocks_in_conditions.rs

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_block_with_applicability;
3-
use clippy_utils::ty::implements_trait;
4-
use clippy_utils::visitors::{for_each_expr_without_closures, Descend};
5-
use clippy_utils::{get_parent_expr, higher, is_from_proc_macro};
6-
use core::ops::ControlFlow;
3+
use clippy_utils::{higher, is_from_proc_macro};
74
use rustc_errors::Applicability;
85
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
96
use rustc_lint::{LateContext, LateLintPass, LintContext};
107
use rustc_middle::lint::in_external_macro;
118
use rustc_session::declare_lint_pass;
12-
use rustc_span::sym;
139

1410
declare_clippy_lint! {
1511
/// ### What it does
@@ -124,30 +120,6 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
124120
);
125121
}
126122
}
127-
} else {
128-
let _: Option<!> = for_each_expr_without_closures(cond, |e| {
129-
if let ExprKind::Closure(closure) = e.kind {
130-
// do not lint if the closure is called using an iterator (see #1141)
131-
if let Some(parent) = get_parent_expr(cx, e)
132-
&& let ExprKind::MethodCall(_, self_arg, _, _) = &parent.kind
133-
&& let caller = cx.typeck_results().expr_ty(self_arg)
134-
&& let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator)
135-
&& implements_trait(cx, caller, iter_id, &[])
136-
{
137-
return ControlFlow::Continue(Descend::No);
138-
}
139-
140-
let body = cx.tcx.hir().body(closure.body);
141-
let ex = &body.value;
142-
if let ExprKind::Block(block, _) = ex.kind {
143-
if !body.value.span.from_expansion() && !block.stmts.is_empty() {
144-
span_lint(cx, BLOCKS_IN_CONDITIONS, ex.span, complex_block_message.clone());
145-
return ControlFlow::Continue(Descend::No);
146-
}
147-
}
148-
}
149-
ControlFlow::Continue(Descend::Yes)
150-
});
151123
}
152124
}
153125
}

tests/ui/blocks_in_conditions.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,17 @@ mod issue_12016 {
117117
}
118118
}
119119

120+
fn in_closure() {
121+
let v = vec![1, 2, 3];
122+
if v.into_iter()
123+
.filter(|x| {
124+
let y = x + 1;
125+
y > 3
126+
})
127+
.any(|x| x == 5)
128+
{
129+
println!("contains 4!");
130+
}
131+
}
132+
120133
fn main() {}

tests/ui/blocks_in_conditions.rs

+13
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,17 @@ mod issue_12016 {
117117
}
118118
}
119119

120+
fn in_closure() {
121+
let v = vec![1, 2, 3];
122+
if v.into_iter()
123+
.filter(|x| {
124+
let y = x + 1;
125+
y > 3
126+
})
127+
.any(|x| x == 5)
128+
{
129+
println!("contains 4!");
130+
}
131+
}
132+
120133
fn main() {}

tests/ui/blocks_in_conditions_closure.rs

-89
This file was deleted.

tests/ui/blocks_in_conditions_closure.stderr

-39
This file was deleted.

0 commit comments

Comments
 (0)