From 10191106d72cc96a0008b31a51b7140554fc79b4 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Tue, 12 Aug 2025 01:01:18 +0200 Subject: [PATCH 1/6] import `FnKind` completely --- clippy_lints/src/functions/too_many_arguments.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/functions/too_many_arguments.rs b/clippy_lints/src/functions/too_many_arguments.rs index 48d050aa36aa..fe61ced155b1 100644 --- a/clippy_lints/src/functions/too_many_arguments.rs +++ b/clippy_lints/src/functions/too_many_arguments.rs @@ -1,5 +1,6 @@ use rustc_abi::ExternAbi; -use rustc_hir::{self as hir, intravisit}; +use rustc_hir as hir; +use rustc_hir::intravisit::FnKind; use rustc_lint::LateContext; use rustc_span::Span; @@ -10,7 +11,7 @@ use super::TOO_MANY_ARGUMENTS; pub(super) fn check_fn( cx: &LateContext<'_>, - kind: intravisit::FnKind<'_>, + kind: FnKind<'_>, decl: &hir::FnDecl<'_>, span: Span, hir_id: hir::HirId, @@ -20,7 +21,7 @@ pub(super) fn check_fn( if !is_trait_impl_item(cx, hir_id) { // don't lint extern functions decls, it's not their fault either match kind { - intravisit::FnKind::Method( + FnKind::Method( _, &hir::FnSig { header: hir::FnHeader { @@ -29,7 +30,7 @@ pub(super) fn check_fn( .. }, ) - | intravisit::FnKind::ItemFn( + | FnKind::ItemFn( _, _, hir::FnHeader { From 4570f51ffffe7e0d1107d20047c457b19eba81d8 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Tue, 12 Aug 2025 01:03:23 +0200 Subject: [PATCH 2/6] save `header` as an intermediate variable --- .../src/functions/too_many_arguments.rs | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/clippy_lints/src/functions/too_many_arguments.rs b/clippy_lints/src/functions/too_many_arguments.rs index fe61ced155b1..38b0f0814d94 100644 --- a/clippy_lints/src/functions/too_many_arguments.rs +++ b/clippy_lints/src/functions/too_many_arguments.rs @@ -21,27 +21,18 @@ pub(super) fn check_fn( if !is_trait_impl_item(cx, hir_id) { // don't lint extern functions decls, it's not their fault either match kind { - FnKind::Method( - _, - &hir::FnSig { - header: hir::FnHeader { - abi: ExternAbi::Rust, .. - }, - .. - }, - ) - | FnKind::ItemFn( - _, - _, - hir::FnHeader { + FnKind::Method(_, &hir::FnSig { header, .. }) | FnKind::ItemFn(_, _, header) + if let hir::FnHeader { abi: ExternAbi::Rust, .. - }, - ) => check_arg_number( - cx, - decl, - span.with_hi(decl.output.span().hi()), - too_many_arguments_threshold, - ), + } = header => + { + check_arg_number( + cx, + decl, + span.with_hi(decl.output.span().hi()), + too_many_arguments_threshold, + ) + }, _ => {}, } } From 256429a5e3cf8ef9ba34411449bfbaf2a5add67d Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Tue, 12 Aug 2025 01:03:38 +0200 Subject: [PATCH 3/6] match on `header.abi` directly --- clippy_lints/src/functions/too_many_arguments.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/clippy_lints/src/functions/too_many_arguments.rs b/clippy_lints/src/functions/too_many_arguments.rs index 38b0f0814d94..7ab8fd5508fe 100644 --- a/clippy_lints/src/functions/too_many_arguments.rs +++ b/clippy_lints/src/functions/too_many_arguments.rs @@ -22,9 +22,7 @@ pub(super) fn check_fn( // don't lint extern functions decls, it's not their fault either match kind { FnKind::Method(_, &hir::FnSig { header, .. }) | FnKind::ItemFn(_, _, header) - if let hir::FnHeader { - abi: ExternAbi::Rust, .. - } = header => + if header.abi == ExternAbi::Rust => { check_arg_number( cx, From 8f6ef40c9b2ff29d5bbd32ffe508a5e815b274f6 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Tue, 12 Aug 2025 01:05:41 +0200 Subject: [PATCH 4/6] use `FnKind::header` --- .../src/functions/too_many_arguments.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/functions/too_many_arguments.rs b/clippy_lints/src/functions/too_many_arguments.rs index 7ab8fd5508fe..28cf88833f2c 100644 --- a/clippy_lints/src/functions/too_many_arguments.rs +++ b/clippy_lints/src/functions/too_many_arguments.rs @@ -20,18 +20,13 @@ pub(super) fn check_fn( // don't warn for implementations, it's not their fault if !is_trait_impl_item(cx, hir_id) { // don't lint extern functions decls, it's not their fault either - match kind { - FnKind::Method(_, &hir::FnSig { header, .. }) | FnKind::ItemFn(_, _, header) - if header.abi == ExternAbi::Rust => - { - check_arg_number( - cx, - decl, - span.with_hi(decl.output.span().hi()), - too_many_arguments_threshold, - ) - }, - _ => {}, + if kind.header().is_some_and(|header| header.abi == ExternAbi::Rust) { + check_arg_number( + cx, + decl, + span.with_hi(decl.output.span().hi()), + too_many_arguments_threshold, + ); } } } From c0f379eda4df3d1f770f9fcb7a8b30bf9a810c4b Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Tue, 12 Aug 2025 01:18:44 +0200 Subject: [PATCH 5/6] nest `if`s --- .../src/functions/too_many_arguments.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/functions/too_many_arguments.rs b/clippy_lints/src/functions/too_many_arguments.rs index 28cf88833f2c..8a2b157043e9 100644 --- a/clippy_lints/src/functions/too_many_arguments.rs +++ b/clippy_lints/src/functions/too_many_arguments.rs @@ -18,16 +18,16 @@ pub(super) fn check_fn( too_many_arguments_threshold: u64, ) { // don't warn for implementations, it's not their fault - if !is_trait_impl_item(cx, hir_id) { + if !is_trait_impl_item(cx, hir_id) // don't lint extern functions decls, it's not their fault either - if kind.header().is_some_and(|header| header.abi == ExternAbi::Rust) { - check_arg_number( - cx, - decl, - span.with_hi(decl.output.span().hi()), - too_many_arguments_threshold, - ); - } + && kind.header().is_some_and(|header| header.abi == ExternAbi::Rust) + { + check_arg_number( + cx, + decl, + span.with_hi(decl.output.span().hi()), + too_many_arguments_threshold, + ); } } From 31fc027096428c478225d51dcf182b7e6e90cb36 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Tue, 12 Aug 2025 16:33:22 +0200 Subject: [PATCH 6/6] use `cx.tcx.def_span` as recommended in https://github.com/rust-lang/rust-clippy/pull/15461#discussion_r2269961969 --- clippy_lints/src/functions/mod.rs | 2 +- clippy_lints/src/functions/too_many_arguments.rs | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/functions/mod.rs b/clippy_lints/src/functions/mod.rs index 6051dc9479ba..0e76cc1e8d49 100644 --- a/clippy_lints/src/functions/mod.rs +++ b/clippy_lints/src/functions/mod.rs @@ -502,7 +502,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions { def_id: LocalDefId, ) { let hir_id = cx.tcx.local_def_id_to_hir_id(def_id); - too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold); + too_many_arguments::check_fn(cx, kind, decl, hir_id, def_id, self.too_many_arguments_threshold); too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold); not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, def_id); misnamed_getters::check_fn(cx, kind, decl, body, span); diff --git a/clippy_lints/src/functions/too_many_arguments.rs b/clippy_lints/src/functions/too_many_arguments.rs index 8a2b157043e9..6c3c3d354ecc 100644 --- a/clippy_lints/src/functions/too_many_arguments.rs +++ b/clippy_lints/src/functions/too_many_arguments.rs @@ -1,5 +1,6 @@ use rustc_abi::ExternAbi; use rustc_hir as hir; +use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::FnKind; use rustc_lint::LateContext; use rustc_span::Span; @@ -13,8 +14,8 @@ pub(super) fn check_fn( cx: &LateContext<'_>, kind: FnKind<'_>, decl: &hir::FnDecl<'_>, - span: Span, hir_id: hir::HirId, + def_id: LocalDefId, too_many_arguments_threshold: u64, ) { // don't warn for implementations, it's not their fault @@ -22,12 +23,7 @@ pub(super) fn check_fn( // don't lint extern functions decls, it's not their fault either && kind.header().is_some_and(|header| header.abi == ExternAbi::Rust) { - check_arg_number( - cx, - decl, - span.with_hi(decl.output.span().hi()), - too_many_arguments_threshold, - ); + check_arg_number(cx, decl, cx.tcx.def_span(def_id), too_many_arguments_threshold); } }