1+ mod too_many_arguments;
2+
13use clippy_utils:: diagnostics:: { span_lint, span_lint_and_help, span_lint_and_then} ;
24use clippy_utils:: source:: { snippet, snippet_opt} ;
35use clippy_utils:: ty:: { is_must_use_ty, is_type_diagnostic_item, type_is_unsafe_function} ;
46use 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} ;
810use if_chain:: if_chain;
911use rustc_ast:: ast:: Attribute ;
@@ -17,9 +19,7 @@ use rustc_middle::hir::map::Map;
1719use rustc_middle:: lint:: in_external_macro;
1820use rustc_middle:: ty:: { self , Ty } ;
1921use 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 } ;
2323use rustc_typeck:: hir_ty_to_ty;
2424
2525declare_clippy_lint ! {
@@ -222,13 +222,16 @@ declare_clippy_lint! {
222222
223223#[ derive( Copy , Clone ) ]
224224pub struct Functions {
225- threshold : u64 ,
226- max_lines : u64 ,
225+ too_many_arguments_threshold : u64 ,
226+ too_many_lines_threshold : u64 ,
227227}
228228
229229impl 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
374358impl < ' 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 }
0 commit comments