|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_then, is_in_test_function}; |
| 2 | + |
| 3 | +use rustc_hir::{intravisit::FnKind, Body, Generics, HirId}; |
| 4 | +use rustc_lint::LateContext; |
| 5 | +use rustc_span::Span; |
| 6 | + |
| 7 | +use super::IMPL_TRAIT_PARAM; |
| 8 | + |
| 9 | +pub(super) fn check_fn<'tcx>(cx: &LateContext<'_>, kind: &'tcx FnKind<'_>, body: &'tcx Body<'_>, hir_id: HirId) { |
| 10 | + if cx.tcx.visibility(cx.tcx.hir().body_owner_def_id(body.id())).is_public() && !is_in_test_function(cx.tcx, hir_id) |
| 11 | + { |
| 12 | + if let FnKind::ItemFn(ident, generics, _) = kind { |
| 13 | + for param in generics.params { |
| 14 | + if param.is_impl_trait() |
| 15 | + && !param.name.ident().as_str().contains('<') |
| 16 | + && !param.name.ident().as_str().contains('(') |
| 17 | + { |
| 18 | + // No generics with nested generics, and no generics like FnMut(x) |
| 19 | + span_lint_and_then( |
| 20 | + cx, |
| 21 | + IMPL_TRAIT_PARAM, |
| 22 | + param.span, |
| 23 | + &format!("'{}' in the function's parameters", param.name.ident().as_str()), |
| 24 | + |diag| { |
| 25 | + let next_letter = next_valid_letter(generics); |
| 26 | + if let Some(gen_span) = generics.span_for_param_suggestion() { |
| 27 | + diag.span_suggestion_with_style( |
| 28 | + gen_span, |
| 29 | + format!( |
| 30 | + "create a generic type here and replace that `{}` with `{}`", |
| 31 | + param.name.ident().as_str(), |
| 32 | + next_letter |
| 33 | + ), |
| 34 | + ", T: Trait", |
| 35 | + rustc_errors::Applicability::MaybeIncorrect, |
| 36 | + rustc_errors::SuggestionStyle::ShowAlways, |
| 37 | + ); |
| 38 | + } else { |
| 39 | + // multispan.push_span_label(param.span, format!("Replace this with `{}`", |
| 40 | + // next_letter)); |
| 41 | + |
| 42 | + diag.span_suggestion_with_style( |
| 43 | + Span::new( |
| 44 | + body.params[0].span.lo() - rustc_span::BytePos(1), |
| 45 | + ident.span.hi(), |
| 46 | + ident.span.ctxt(), |
| 47 | + ident.span.parent(), |
| 48 | + ), |
| 49 | + format!( |
| 50 | + "create a generic type here and replace that '{}' with `{}`", |
| 51 | + param.name.ident().as_str(), |
| 52 | + next_letter |
| 53 | + ), |
| 54 | + "<T: Trait>", |
| 55 | + rustc_errors::Applicability::MaybeIncorrect, |
| 56 | + rustc_errors::SuggestionStyle::ShowAlways, |
| 57 | + ); |
| 58 | + } |
| 59 | + }, |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +fn next_valid_letter(generics: &Generics<'_>) -> char { |
| 68 | + let mut generics_names = Vec::new(); |
| 69 | + |
| 70 | + generics.params.iter().for_each(|param| { |
| 71 | + generics_names.push(param.name.ident().as_str().to_owned()); |
| 72 | + }); |
| 73 | + |
| 74 | + // If T exists, try with U, then with V, and so on... |
| 75 | + let mut current_letter = 84u32; // ASCII code for "T" |
| 76 | + while generics_names.contains(&String::from(char::from_u32(current_letter).unwrap())) { |
| 77 | + current_letter += 1; |
| 78 | + if current_letter == 91 { |
| 79 | + // ASCII code for "Z" |
| 80 | + current_letter = 65; |
| 81 | + } else if current_letter == 83 { |
| 82 | + // ASCII "S" |
| 83 | + current_letter = 97; // "a" |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + char::from_u32(current_letter).unwrap() |
| 88 | +} |
0 commit comments