Add placeholders, remove name suggesting

This commit is contained in:
blyxyas 2023-02-18 20:05:30 +01:00
parent 6ef34bf009
commit 89fde4abf2
No known key found for this signature in database
GPG Key ID: 4D38170B5A2FC334
2 changed files with 9 additions and 33 deletions

View File

@ -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}: {}", &param.name.ident().as_str()[5..]),
rustc_errors::Applicability::MaybeIncorrect,
format!(", {{ /* Generic name */ }}: {}", &param.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}: {}>", &param.name.ident().as_str()[5..]),
rustc_errors::Applicability::MaybeIncorrect,
format!("<{{ /* Generic name */ }}: {}>", &param.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()
}

View File

@ -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<T: Trait>(_: 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: Trait>(_: C, _: impl Trait) {}
|
help: add a type paremeter
|
LL | pub fn c<C: Trait, T: Trait>(_: C, _: impl Trait) {}
| ++++++++++
LL | pub fn c<C: Trait, { /* Generic name */ }: Trait>(_: C, _: impl Trait) {}
| +++++++++++++++++++++++++++++++
error: aborting due to 2 previous errors