diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2024-12-17 13:34:04 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-17 13:34:04 +0000 |
| commit | 0f9cc8d58b82ff6d6b9380a10a8cbed22734eac0 (patch) | |
| tree | 86b6435dc2527e683aef1d5216517ffcbdb7eeca | |
| parent | 77c9dddecb91e2642ff57837a4e77b51e4c3ea63 (diff) | |
| parent | bd078ad1b5fc9882a115cf4cc98f01c0f913bfd4 (diff) | |
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
| -rw-r--r-- | clippy_lints/src/methods/filter_map_identity.rs | 10 | ||||
| -rw-r--r-- | tests/ui/filter_map_identity.fixed | 5 | ||||
| -rw-r--r-- | tests/ui/filter_map_identity.rs | 5 |
3 files changed, 20 insertions, 0 deletions
diff --git a/clippy_lints/src/methods/filter_map_identity.rs b/clippy_lints/src/methods/filter_map_identity.rs index cf7f276dabb..b04d761d486 100644 --- a/clippy_lints/src/methods/filter_map_identity.rs +++ b/clippy_lints/src/methods/filter_map_identity.rs @@ -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}; @@ -21,6 +22,15 @@ 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) { + // check if the iterator is from an empty array, see issue #12653 + if let ExprKind::MethodCall(_, recv, ..) = expr.kind + && let ExprKind::MethodCall(_, recv2, ..) = recv.kind + && let ExprKind::Array(arr) = recv2.kind + && arr.is_empty() + { + return; + } + span_lint_and_sugg( cx, FILTER_MAP_IDENTITY, diff --git a/tests/ui/filter_map_identity.fixed b/tests/ui/filter_map_identity.fixed index f3f6848e5f9..fdd020fcd77 100644 --- a/tests/ui/filter_map_identity.fixed +++ b/tests/ui/filter_map_identity.fixed @@ -81,3 +81,8 @@ fn main() { //~^ ERROR: use of } } + +fn issue12653() -> impl Iterator<Item = u8> { + [].into_iter().filter_map(|x| x) + // No lint +} diff --git a/tests/ui/filter_map_identity.rs b/tests/ui/filter_map_identity.rs index b9aa9c05be8..a626de9f5bb 100644 --- a/tests/ui/filter_map_identity.rs +++ b/tests/ui/filter_map_identity.rs @@ -81,3 +81,8 @@ fn main() { //~^ ERROR: use of } } + +fn issue12653() -> impl Iterator<Item = u8> { + [].into_iter().filter_map(|x| x) + // No lint +} |
