Skip to content

Commit

Permalink
Add placeholders, remove name suggesting
Browse files Browse the repository at this point in the history
  • Loading branch information
blyxyas committed Feb 18, 2023
1 parent 6ef34bf commit 89fde4a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
34 changes: 5 additions & 29 deletions clippy_lints/src/functions/impl_trait_in_params.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {
Expand All @@ -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,
);
}
Expand All @@ -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()
}
8 changes: 4 additions & 4 deletions tests/ui/impl_trait_in_params.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 89fde4a

Please sign in to comment.