diff options
| author | lapla-cogito <me@lapla.dev> | 2024-12-14 21:19:49 +0900 |
|---|---|---|
| committer | lapla-cogito <me@lapla.dev> | 2024-12-14 22:49:49 +0900 |
| commit | bd078ad1b5fc9882a115cf4cc98f01c0f913bfd4 (patch) | |
| tree | 5eb77b3ea9379abcc05b2ceb9a09624b2b882f04 | |
| parent | 13463cb072e1ae70677329931b434b4070710e2a (diff) | |
| download | rust-bd078ad1b5fc9882a115cf4cc98f01c0f913bfd4.tar.gz rust-bd078ad1b5fc9882a115cf4cc98f01c0f913bfd4.zip | |
don't lint for creating an iterator from an empty array in filter_map_identity lint
| -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 +} |
