]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/functions/too_many_arguments.rs
Auto merge of #92740 - cuviper:update-rayons, r=Mark-Simulacrum
[rust.git] / clippy_lints / src / functions / too_many_arguments.rs
1 use rustc_hir::{self as hir, intravisit};
2 use rustc_lint::LateContext;
3 use rustc_span::Span;
4 use rustc_target::spec::abi::Abi;
5
6 use clippy_utils::diagnostics::span_lint;
7 use clippy_utils::is_trait_impl_item;
8
9 use super::TOO_MANY_ARGUMENTS;
10
11 pub(super) fn check_fn(
12     cx: &LateContext<'_>,
13     kind: intravisit::FnKind<'_>,
14     decl: &hir::FnDecl<'_>,
15     span: Span,
16     hir_id: hir::HirId,
17     too_many_arguments_threshold: u64,
18 ) {
19     // don't warn for implementations, it's not their fault
20     if !is_trait_impl_item(cx, hir_id) {
21         // don't lint extern functions decls, it's not their fault either
22         match kind {
23             intravisit::FnKind::Method(
24                 _,
25                 &hir::FnSig {
26                     header: hir::FnHeader { abi: Abi::Rust, .. },
27                     ..
28                 },
29                 _,
30             )
31             | intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _) => check_arg_number(
32                 cx,
33                 decl,
34                 span.with_hi(decl.output.span().hi()),
35                 too_many_arguments_threshold,
36             ),
37             _ => {},
38         }
39     }
40 }
41
42 pub(super) fn check_trait_item(cx: &LateContext<'_>, item: &hir::TraitItem<'_>, too_many_arguments_threshold: u64) {
43     if let hir::TraitItemKind::Fn(ref sig, _) = item.kind {
44         // don't lint extern functions decls, it's not their fault
45         if sig.header.abi == Abi::Rust {
46             check_arg_number(
47                 cx,
48                 sig.decl,
49                 item.span.with_hi(sig.decl.output.span().hi()),
50                 too_many_arguments_threshold,
51             );
52         }
53     }
54 }
55
56 fn check_arg_number(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, fn_span: Span, too_many_arguments_threshold: u64) {
57     let args = decl.inputs.len() as u64;
58     if args > too_many_arguments_threshold {
59         span_lint(
60             cx,
61             TOO_MANY_ARGUMENTS,
62             fn_span,
63             &format!(
64                 "this function has too many arguments ({}/{})",
65                 args, too_many_arguments_threshold
66             ),
67         );
68     }
69 }