]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/functions/impl_trait_in_params.rs
New upstream version 1.69.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / functions / impl_trait_in_params.rs
1 use clippy_utils::{diagnostics::span_lint_and_then, is_in_test_function};
2
3 use rustc_hir::{intravisit::FnKind, Body, HirId};
4 use rustc_lint::LateContext;
5 use rustc_span::Span;
6
7 use super::IMPL_TRAIT_IN_PARAMS;
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 // No generics with nested generics, and no generics like FnMut(x)
16 span_lint_and_then(
17 cx,
18 IMPL_TRAIT_IN_PARAMS,
19 param.span,
20 "'`impl Trait` used as a function parameter'",
21 |diag| {
22 if let Some(gen_span) = generics.span_for_param_suggestion() {
23 diag.span_suggestion_with_style(
24 gen_span,
25 "add a type paremeter",
26 format!(", {{ /* Generic name */ }}: {}", &param.name.ident().as_str()[5..]),
27 rustc_errors::Applicability::HasPlaceholders,
28 rustc_errors::SuggestionStyle::ShowAlways,
29 );
30 } else {
31 diag.span_suggestion_with_style(
32 Span::new(
33 body.params[0].span.lo() - rustc_span::BytePos(1),
34 ident.span.hi(),
35 ident.span.ctxt(),
36 ident.span.parent(),
37 ),
38 "add a type paremeter",
39 format!("<{{ /* Generic name */ }}: {}>", &param.name.ident().as_str()[5..]),
40 rustc_errors::Applicability::HasPlaceholders,
41 rustc_errors::SuggestionStyle::ShowAlways,
42 );
43 }
44 },
45 );
46 }
47 }
48 }
49 }
50 }