Skip to content

Commit 2f19f5f

Browse files
committed
Auto merge of #6741 - ThibsG:BlockInIfConditions1141, r=flip1995
Do not lint when the closure is called using an iterator Fix FP when the closure is used in an iterator for `blocks_in_if_conditions` lint FIxes: #1141 changelog: none
2 parents 9c3b43e + 1202550 commit 2f19f5f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

clippy_lints/src/blocks_in_if_conditions.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::utils::{differing_macro_contexts, snippet_block_with_applicability, span_lint, span_lint_and_sugg};
1+
use crate::utils::{
2+
differing_macro_contexts, get_parent_expr, get_trait_def_id, implements_trait, paths,
3+
snippet_block_with_applicability, span_lint, span_lint_and_sugg,
4+
};
5+
use if_chain::if_chain;
26
use rustc_errors::Applicability;
37
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
48
use rustc_hir::{BlockCheckMode, Expr, ExprKind};
@@ -52,6 +56,18 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
5256

5357
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
5458
if let ExprKind::Closure(_, _, eid, _, _) = expr.kind {
59+
// do not lint if the closure is called using an iterator (see #1141)
60+
if_chain! {
61+
if let Some(parent) = get_parent_expr(self.cx, expr);
62+
if let ExprKind::MethodCall(_, _, args, _) = parent.kind;
63+
let caller = self.cx.typeck_results().expr_ty(&args[0]);
64+
if let Some(iter_id) = get_trait_def_id(self.cx, &paths::ITERATOR);
65+
if implements_trait(self.cx, caller, iter_id, &[]);
66+
then {
67+
return;
68+
}
69+
}
70+
5571
let body = self.cx.tcx.hir().body(eid);
5672
let ex = &body.value;
5773
if matches!(ex.kind, ExprKind::Block(_, _)) && !body.value.span.from_expansion() {

tests/ui/blocks_in_if_conditions_closure.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,13 @@ fn macro_in_closure() {
4444
}
4545
}
4646

47-
fn main() {}
47+
#[rustfmt::skip]
48+
fn main() {
49+
let mut range = 0..10;
50+
range.all(|i| {i < 10} );
51+
52+
let v = vec![1, 2, 3];
53+
if v.into_iter().any(|x| {x == 4}) {
54+
println!("contains 4!");
55+
}
56+
}

0 commit comments

Comments
 (0)