Skip to content

Commit 0f9cc8d

Browse files
authored
Don't trigger filter_map_identity with an iterator from an empty array (#13826)
fix #12653 changelog: [`filter_map_identity`]: don't lint for creating an iterator from an empty array
2 parents 77c9ddd + bd078ad commit 0f9cc8d

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

clippy_lints/src/methods/filter_map_identity.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::{is_expr_identity_function, is_expr_untyped_identity_function, is_trait_method};
33
use rustc_errors::Applicability;
44
use rustc_hir as hir;
5+
use rustc_hir::ExprKind;
56
use rustc_lint::LateContext;
67
use rustc_span::{Span, sym};
78

@@ -21,6 +22,15 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_map_arg:
2122
if is_trait_method(cx, expr, sym::Iterator)
2223
&& let Some(applicability) = is_identity(cx, filter_map_arg)
2324
{
25+
// check if the iterator is from an empty array, see issue #12653
26+
if let ExprKind::MethodCall(_, recv, ..) = expr.kind
27+
&& let ExprKind::MethodCall(_, recv2, ..) = recv.kind
28+
&& let ExprKind::Array(arr) = recv2.kind
29+
&& arr.is_empty()
30+
{
31+
return;
32+
}
33+
2434
span_lint_and_sugg(
2535
cx,
2636
FILTER_MAP_IDENTITY,

tests/ui/filter_map_identity.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ fn main() {
8181
//~^ ERROR: use of
8282
}
8383
}
84+
85+
fn issue12653() -> impl Iterator<Item = u8> {
86+
[].into_iter().filter_map(|x| x)
87+
// No lint
88+
}

tests/ui/filter_map_identity.rs

+5
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ fn main() {
8181
//~^ ERROR: use of
8282
}
8383
}
84+
85+
fn issue12653() -> impl Iterator<Item = u8> {
86+
[].into_iter().filter_map(|x| x)
87+
// No lint
88+
}

0 commit comments

Comments
 (0)