From 89fde4abf2c689c0ad5a09cc423a0a7be475d6ee Mon Sep 17 00:00:00 2001 From: blyxyas Date: Sat, 18 Feb 2023 20:05:30 +0100 Subject: [PATCH] Add placeholders, remove name suggesting --- .../src/functions/impl_trait_in_params.rs | 34 +++---------------- tests/ui/impl_trait_in_params.stderr | 8 ++--- 2 files changed, 9 insertions(+), 33 deletions(-) diff --git a/clippy_lints/src/functions/impl_trait_in_params.rs b/clippy_lints/src/functions/impl_trait_in_params.rs index 1f00e357bfa..2811a73f6c1 100644 --- a/clippy_lints/src/functions/impl_trait_in_params.rs +++ b/clippy_lints/src/functions/impl_trait_in_params.rs @@ -1,6 +1,6 @@ use clippy_utils::{diagnostics::span_lint_and_then, is_in_test_function}; -use rustc_hir::{intravisit::FnKind, Body, Generics, HirId}; +use rustc_hir::{intravisit::FnKind, Body, HirId}; use rustc_lint::LateContext; use rustc_span::Span; @@ -19,13 +19,12 @@ pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: param.span, "'`impl Trait` used as a function parameter'", |diag| { - let next_letter = next_valid_letter(generics); if let Some(gen_span) = generics.span_for_param_suggestion() { diag.span_suggestion_with_style( gen_span, "add a type paremeter", - format!(", {next_letter}: {}", ¶m.name.ident().as_str()[5..]), - rustc_errors::Applicability::MaybeIncorrect, + format!(", {{ /* Generic name */ }}: {}", ¶m.name.ident().as_str()[5..]), + rustc_errors::Applicability::HasPlaceholders, rustc_errors::SuggestionStyle::ShowAlways, ); } else { @@ -37,8 +36,8 @@ pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: ident.span.parent(), ), "add a type paremeter", - format!("<{next_letter}: {}>", ¶m.name.ident().as_str()[5..]), - rustc_errors::Applicability::MaybeIncorrect, + format!("<{{ /* Generic name */ }}: {}>", ¶m.name.ident().as_str()[5..]), + rustc_errors::Applicability::HasPlaceholders, rustc_errors::SuggestionStyle::ShowAlways, ); } @@ -49,26 +48,3 @@ pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: } } } - -fn next_valid_letter(generics: &Generics<'_>) -> char { - let mut generics_names = Vec::new(); - - generics.params.iter().for_each(|param| { - generics_names.push(param.name.ident().as_str().to_owned()); - }); - - // If T exists, try with U, then with V, and so on... - let mut current_letter = 84u32; // ASCII code for "T" - while generics_names.contains(&String::from(char::from_u32(current_letter).unwrap())) { - current_letter += 1; - if current_letter == 91 { - // ASCII code for "Z" - current_letter = 65; - } else if current_letter == 83 { - // ASCII "S" - current_letter = 97; // "a" - }; - } - - char::from_u32(current_letter).unwrap() -} diff --git a/tests/ui/impl_trait_in_params.stderr b/tests/ui/impl_trait_in_params.stderr index c0bcdfd6d42..acfcc21445e 100644 --- a/tests/ui/impl_trait_in_params.stderr +++ b/tests/ui/impl_trait_in_params.stderr @@ -7,8 +7,8 @@ LL | pub fn a(_: impl Trait) {} = note: `-D clippy::impl-trait-in-params` implied by `-D warnings` help: add a type paremeter | -LL | pub fn a(_: impl Trait) {} - | ++++++++++ +LL | pub fn a<{ /* Generic name */ }: Trait>(_: impl Trait) {} + | +++++++++++++++++++++++++++++++ error: '`impl Trait` used as a function parameter' --> $DIR/impl_trait_in_params.rs:9:29 @@ -18,8 +18,8 @@ LL | pub fn c(_: C, _: impl Trait) {} | help: add a type paremeter | -LL | pub fn c(_: C, _: impl Trait) {} - | ++++++++++ +LL | pub fn c(_: C, _: impl Trait) {} + | +++++++++++++++++++++++++++++++ error: aborting due to 2 previous errors