]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/functions/too_many_arguments.rs
Auto merge of #8937 - Jarcho:merge_match_passes, r=llogiq
[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             | intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }) => check_arg_number(
31                 cx,
32                 decl,
33                 span.with_hi(decl.output.span().hi()),
34                 too_many_arguments_threshold,
35             ),
36             _ => {},
37         }
38     }
39 }
40
41 pub(super) fn check_trait_item(cx: &LateContext<'_>, item: &hir::TraitItem<'_>, too_many_arguments_threshold: u64) {
42     if let hir::TraitItemKind::Fn(ref sig, _) = item.kind {
43         // don't lint extern functions decls, it's not their fault
44         if sig.header.abi == Abi::Rust {
45             check_arg_number(
46                 cx,
47                 sig.decl,
48                 item.span.with_hi(sig.decl.output.span().hi()),
49                 too_many_arguments_threshold,
50             );
51         }
52     }
53 }
54
55 fn check_arg_number(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, fn_span: Span, too_many_arguments_threshold: u64) {
56     let args = decl.inputs.len() as u64;
57     if args > too_many_arguments_threshold {
58         span_lint(
59             cx,
60             TOO_MANY_ARGUMENTS,
61             fn_span,
62             &format!(
63                 "this function has too many arguments ({}/{})",
64                 args, too_many_arguments_threshold
65             ),
66         );
67     }
68 }