From 0b867cf5f61a29e6710083eb8e89a819911cefe6 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sat, 30 Nov 2024 21:40:26 +0100 Subject: [PATCH] Add `Iterator::map` to `useless_conversion` --- clippy_lints/src/useless_conversion.rs | 3 +++ tests/ui/useless_conversion.fixed | 8 +++++++- tests/ui/useless_conversion.rs | 8 +++++++- tests/ui/useless_conversion.stderr | 14 +++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/useless_conversion.rs b/clippy_lints/src/useless_conversion.rs index 8bf8004ef876..ccf04ab50f20 100644 --- a/clippy_lints/src/useless_conversion.rs +++ b/clippy_lints/src/useless_conversion.rs @@ -433,5 +433,8 @@ fn has_eligible_receiver(cx: &LateContext<'_>, recv: &Expr<'_>, expr: &Expr<'_>) return true; } } + if is_trait_method(cx, expr, sym::Iterator) { + return true; + } false } diff --git a/tests/ui/useless_conversion.fixed b/tests/ui/useless_conversion.fixed index 1d2fdc90419c..2f7edd92bb7c 100644 --- a/tests/ui/useless_conversion.fixed +++ b/tests/ui/useless_conversion.fixed @@ -328,11 +328,17 @@ fn direct_application() { Self } } + let _: Vec = [1u32].into_iter().collect(); + //~^ useless_conversion // No lint for those let _: Result = test_issue_3913().map(Into::into); let _: Result<(), Absorb> = test_issue_3913().map_err(Into::into); let _: Result = test_issue_3913().map(From::from); let _: Result<(), Absorb> = test_issue_3913().map_err(From::from); - let _: Vec = [1u32].into_iter().map(Into::into).collect(); +} + +fn gen_identity(x: [T; 3]) -> Vec { + x.into_iter().collect() + //~^ useless_conversion } diff --git a/tests/ui/useless_conversion.rs b/tests/ui/useless_conversion.rs index 3719dc6c3fbe..eacdf77f9052 100644 --- a/tests/ui/useless_conversion.rs +++ b/tests/ui/useless_conversion.rs @@ -328,11 +328,17 @@ fn direct_application() { Self } } + let _: Vec = [1u32].into_iter().map(Into::into).collect(); + //~^ useless_conversion // No lint for those let _: Result = test_issue_3913().map(Into::into); let _: Result<(), Absorb> = test_issue_3913().map_err(Into::into); let _: Result = test_issue_3913().map(From::from); let _: Result<(), Absorb> = test_issue_3913().map_err(From::from); - let _: Vec = [1u32].into_iter().map(Into::into).collect(); +} + +fn gen_identity(x: [T; 3]) -> Vec { + x.into_iter().map(Into::into).collect() + //~^ useless_conversion } diff --git a/tests/ui/useless_conversion.stderr b/tests/ui/useless_conversion.stderr index f1e18f6b0031..6aeb382902ba 100644 --- a/tests/ui/useless_conversion.stderr +++ b/tests/ui/useless_conversion.stderr @@ -262,5 +262,17 @@ error: useless conversion to the same type: `()` LL | let _: ControlFlow<()> = c.map_continue(Into::into); | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing -error: aborting due to 34 previous errors +error: useless conversion to the same type: `u32` + --> tests/ui/useless_conversion.rs:331:41 + | +LL | let _: Vec = [1u32].into_iter().map(Into::into).collect(); + | ^^^^^^^^^^^^^^^^ help: consider removing + +error: useless conversion to the same type: `T` + --> tests/ui/useless_conversion.rs:342:18 + | +LL | x.into_iter().map(Into::into).collect() + | ^^^^^^^^^^^^^^^^ help: consider removing + +error: aborting due to 36 previous errors