]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/functions/too_many_arguments.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / 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<'tcx>,
13 kind: intravisit::FnKind<'tcx>,
14 decl: &'tcx 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(
43 cx: &LateContext<'tcx>,
44 item: &'tcx hir::TraitItem<'_>,
45 too_many_arguments_threshold: u64,
46 ) {
47 if let hir::TraitItemKind::Fn(ref sig, _) = item.kind {
48 // don't lint extern functions decls, it's not their fault
49 if sig.header.abi == Abi::Rust {
50 check_arg_number(
51 cx,
52 sig.decl,
53 item.span.with_hi(sig.decl.output.span().hi()),
54 too_many_arguments_threshold,
55 );
56 }
57 }
58 }
59
60 fn check_arg_number(cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, fn_span: Span, too_many_arguments_threshold: u64) {
61 let args = decl.inputs.len() as u64;
62 if args > too_many_arguments_threshold {
63 span_lint(
64 cx,
65 TOO_MANY_ARGUMENTS,
66 fn_span,
67 &format!(
68 "this function has too many arguments ({}/{})",
69 args, too_many_arguments_threshold
70 ),
71 );
72 }
73 }