Skip to content

Commit

Permalink
don't lint for creating an iterator from an empty array in filter_map…
Browse files Browse the repository at this point in the history
…_identity lint
  • Loading branch information
lapla-cogito committed Dec 14, 2024
1 parent 13463cb commit d3d24e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clippy_lints/src/methods/filter_map_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::{is_expr_identity_function, is_expr_untyped_identity_function, is_trait_method};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::ExprKind;
use rustc_lint::LateContext;
use rustc_span::{Span, sym};

Expand All @@ -21,6 +22,16 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, filter_map_arg:
if is_trait_method(cx, expr, sym::Iterator)
&& let Some(applicability) = is_identity(cx, filter_map_arg)
{
if let ExprKind::MethodCall(_, recv, ..) = expr.kind {
if let ExprKind::MethodCall(_, recv, ..) = recv.kind {
if let ExprKind::Array(arr) = recv.kind {
if arr.is_empty() {
return;
}
}
}
}

span_lint_and_sugg(
cx,
FILTER_MAP_IDENTITY,
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/filter_map_identity.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ fn main() {
//~^ ERROR: use of
}
}

fn issue12653() -> impl Iterator<Item = u8> {
[].into_iter().filter_map(|x| x)
// No lint
}
5 changes: 5 additions & 0 deletions tests/ui/filter_map_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ fn main() {
//~^ ERROR: use of
}
}

fn issue12653() -> impl Iterator<Item = u8> {
[].into_iter().filter_map(|x| x)
// No lint
}

0 comments on commit d3d24e7

Please sign in to comment.