Skip to content

Commit febf34e

Browse files
committed
Move too_many_arguments to its own module
1 parent 8e56a2b commit febf34e

File tree

3 files changed

+97
-49
lines changed

3 files changed

+97
-49
lines changed

clippy_lints/src/functions.rs renamed to clippy_lints/src/functions/mod.rs

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
mod too_many_arguments;
2+
13
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_then};
24
use clippy_utils::source::{snippet, snippet_opt};
35
use clippy_utils::ty::{is_must_use_ty, is_type_diagnostic_item, type_is_unsafe_function};
46
use clippy_utils::{
5-
attr_by_name, attrs::is_proc_macro, is_trait_impl_item, iter_input_pats, match_def_path, must_use_attr,
6-
path_to_local, return_ty, trait_ref_of_method,
7+
attr_by_name, attrs::is_proc_macro, iter_input_pats, match_def_path, must_use_attr, path_to_local, return_ty,
8+
trait_ref_of_method,
79
};
810
use if_chain::if_chain;
911
use rustc_ast::ast::Attribute;
@@ -17,9 +19,7 @@ use rustc_middle::hir::map::Map;
1719
use rustc_middle::lint::in_external_macro;
1820
use rustc_middle::ty::{self, Ty};
1921
use rustc_session::{declare_tool_lint, impl_lint_pass};
20-
use rustc_span::source_map::Span;
21-
use rustc_span::sym;
22-
use rustc_target::spec::abi::Abi;
22+
use rustc_span::{sym, Span};
2323
use rustc_typeck::hir_ty_to_ty;
2424

2525
declare_clippy_lint! {
@@ -222,13 +222,16 @@ declare_clippy_lint! {
222222

223223
#[derive(Copy, Clone)]
224224
pub struct Functions {
225-
threshold: u64,
226-
max_lines: u64,
225+
too_many_arguments_threshold: u64,
226+
too_many_lines_threshold: u64,
227227
}
228228

229229
impl Functions {
230-
pub fn new(threshold: u64, max_lines: u64) -> Self {
231-
Self { threshold, max_lines }
230+
pub fn new(too_many_arguments_threshold: u64, too_many_lines_threshold: u64) -> Self {
231+
Self {
232+
too_many_arguments_threshold,
233+
too_many_lines_threshold,
234+
}
232235
}
233236
}
234237

@@ -252,31 +255,14 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
252255
span: Span,
253256
hir_id: hir::HirId,
254257
) {
258+
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
259+
255260
let unsafety = match kind {
256261
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _) => unsafety,
257262
intravisit::FnKind::Method(_, sig, _) => sig.header.unsafety,
258263
intravisit::FnKind::Closure => return,
259264
};
260265

261-
// don't warn for implementations, it's not their fault
262-
if !is_trait_impl_item(cx, hir_id) {
263-
// don't lint extern functions decls, it's not their fault either
264-
match kind {
265-
intravisit::FnKind::Method(
266-
_,
267-
&hir::FnSig {
268-
header: hir::FnHeader { abi: Abi::Rust, .. },
269-
..
270-
},
271-
_,
272-
)
273-
| intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _) => {
274-
self.check_arg_number(cx, decl, span.with_hi(decl.output.span().hi()))
275-
},
276-
_ => {},
277-
}
278-
}
279-
280266
Self::check_raw_ptr(cx, unsafety, decl, body, hir_id);
281267
self.check_line_number(cx, span, body);
282268
}
@@ -335,11 +321,9 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
335321
}
336322

337323
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
324+
too_many_arguments::check_trait_item(cx, item, self.too_many_arguments_threshold);
325+
338326
if let hir::TraitItemKind::Fn(ref sig, ref eid) = item.kind {
339-
// don't lint extern functions decls, it's not their fault
340-
if sig.header.abi == Abi::Rust {
341-
self.check_arg_number(cx, &sig.decl, item.span.with_hi(sig.decl.output.span().hi()));
342-
}
343327
let is_public = cx.access_levels.is_exported(item.hir_id());
344328
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
345329
if is_public {
@@ -372,18 +356,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
372356
}
373357

374358
impl<'tcx> Functions {
375-
fn check_arg_number(self, cx: &LateContext<'_>, decl: &hir::FnDecl<'_>, fn_span: Span) {
376-
let args = decl.inputs.len() as u64;
377-
if args > self.threshold {
378-
span_lint(
379-
cx,
380-
TOO_MANY_ARGUMENTS,
381-
fn_span,
382-
&format!("this function has too many arguments ({}/{})", args, self.threshold),
383-
);
384-
}
385-
}
386-
387359
fn check_line_number(self, cx: &LateContext<'_>, span: Span, body: &'tcx hir::Body<'_>) {
388360
if in_external_macro(cx.sess(), span) {
389361
return;
@@ -430,12 +402,15 @@ impl<'tcx> Functions {
430402
}
431403
}
432404

433-
if line_count > self.max_lines {
405+
if line_count > self.too_many_lines_threshold {
434406
span_lint(
435407
cx,
436408
TOO_MANY_LINES,
437409
span,
438-
&format!("this function has too many lines ({}/{})", line_count, self.max_lines),
410+
&format!(
411+
"this function has too many lines ({}/{})",
412+
line_count, self.too_many_lines_threshold
413+
),
439414
)
440415
}
441416
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

clippy_lints/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,9 +1124,9 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11241124
store.register_late_pass(|| box new_without_default::NewWithoutDefault::default());
11251125
let blacklisted_names = conf.blacklisted_names.iter().cloned().collect::<FxHashSet<_>>();
11261126
store.register_late_pass(move || box blacklisted_name::BlacklistedName::new(blacklisted_names.clone()));
1127-
let too_many_arguments_threshold1 = conf.too_many_arguments_threshold;
1128-
let too_many_lines_threshold2 = conf.too_many_lines_threshold;
1129-
store.register_late_pass(move || box functions::Functions::new(too_many_arguments_threshold1, too_many_lines_threshold2));
1127+
let too_many_arguments_threshold = conf.too_many_arguments_threshold;
1128+
let too_many_lines_threshold = conf.too_many_lines_threshold;
1129+
store.register_late_pass(move || box functions::Functions::new(too_many_arguments_threshold, too_many_lines_threshold));
11301130
let doc_valid_idents = conf.doc_valid_idents.iter().cloned().collect::<FxHashSet<_>>();
11311131
store.register_late_pass(move || box doc::DocMarkdown::new(doc_valid_idents.clone()));
11321132
store.register_late_pass(|| box neg_multiply::NegMultiply);

0 commit comments

Comments
 (0)