From 5a7e33e5b25c6ee9b71897f2fd1a84d8c839dcf9 Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Sat, 3 Jun 2023 19:16:39 +0200 Subject: [PATCH] add plural form to useless_conversion if depth > 0 --- clippy_lints/src/useless_conversion.rs | 22 +++++++++++----------- tests/ui/useless_conversion.stderr | 10 +++++----- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/clippy_lints/src/useless_conversion.rs b/clippy_lints/src/useless_conversion.rs index 6f1e9d5f9..c413e92ea 100644 --- a/clippy_lints/src/useless_conversion.rs +++ b/clippy_lints/src/useless_conversion.rs @@ -93,14 +93,13 @@ fn into_iter_call<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>) -> Option< /// Same as [`into_iter_call`], but tries to look for the innermost `.into_iter()` call, e.g.: /// `foo.into_iter().into_iter()` /// ^^^ we want this expression -fn into_iter_deep_call<'hir>(cx: &LateContext<'_>, mut expr: &'hir Expr<'hir>) -> Option<&'hir Expr<'hir>> { - loop { - if let Some(recv) = into_iter_call(cx, expr) { - expr = recv; - } else { - return Some(expr); - } +fn into_iter_deep_call<'hir>(cx: &LateContext<'_>, mut expr: &'hir Expr<'hir>) -> (&'hir Expr<'hir>, usize) { + let mut depth = 0; + while let Some(recv) = into_iter_call(cx, expr) { + expr = recv; + depth += 1; } + (expr, depth) } #[expect(clippy::too_many_lines)] @@ -170,18 +169,19 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion { && let Some(&into_iter_param) = sig.inputs().get(kind.param_pos(arg_pos)) && let ty::Param(param) = into_iter_param.kind() && let Some(span) = into_iter_bound(cx, parent_fn_did, into_iter_did, param.index) + { // Get the "innermost" `.into_iter()` call, e.g. given this expression: // `foo.into_iter().into_iter()` // ^^^ - // We want this span - && let Some(into_iter_recv) = into_iter_deep_call(cx, into_iter_recv) - { + let (into_iter_recv, depth) = into_iter_deep_call(cx, into_iter_recv); + + let plural = if depth == 0 { "" } else { "s" }; let mut applicability = Applicability::MachineApplicable; let sugg = snippet_with_applicability(cx, into_iter_recv.span.source_callsite(), "", &mut applicability).into_owned(); span_lint_and_then(cx, USELESS_CONVERSION, e.span, "explicit call to `.into_iter()` in function argument accepting `IntoIterator`", |diag| { diag.span_suggestion( e.span, - "consider removing `.into_iter()`", + format!("consider removing the `.into_iter()`{plural}"), sugg, applicability, ); diff --git a/tests/ui/useless_conversion.stderr b/tests/ui/useless_conversion.stderr index 68f38faab..4957f73a4 100644 --- a/tests/ui/useless_conversion.stderr +++ b/tests/ui/useless_conversion.stderr @@ -122,7 +122,7 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera --> $DIR/useless_conversion.rs:171:7 | LL | b(vec![1, 2].into_iter()); - | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2]` + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> $DIR/useless_conversion.rs:161:13 @@ -134,7 +134,7 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera --> $DIR/useless_conversion.rs:172:7 | LL | c(vec![1, 2].into_iter()); - | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2]` + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> $DIR/useless_conversion.rs:162:18 @@ -146,7 +146,7 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera --> $DIR/useless_conversion.rs:173:7 | LL | d(vec![1, 2].into_iter()); - | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2]` + | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> $DIR/useless_conversion.rs:165:12 @@ -158,7 +158,7 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera --> $DIR/useless_conversion.rs:176:7 | LL | b(vec![1, 2].into_iter().into_iter()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2]` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`s: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> $DIR/useless_conversion.rs:161:13 @@ -170,7 +170,7 @@ error: explicit call to `.into_iter()` in function argument accepting `IntoItera --> $DIR/useless_conversion.rs:177:7 | LL | b(vec![1, 2].into_iter().into_iter().into_iter()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2]` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`s: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` --> $DIR/useless_conversion.rs:161:13