1
+ mod too_many_arguments;
2
+
1
3
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_help, span_lint_and_then} ;
2
4
use clippy_utils:: source:: { snippet, snippet_opt} ;
3
5
use clippy_utils:: ty:: { is_must_use_ty, is_type_diagnostic_item, type_is_unsafe_function} ;
4
6
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,
7
9
} ;
8
10
use if_chain:: if_chain;
9
11
use rustc_ast:: ast:: Attribute ;
@@ -17,9 +19,7 @@ use rustc_middle::hir::map::Map;
17
19
use rustc_middle:: lint:: in_external_macro;
18
20
use rustc_middle:: ty:: { self , Ty } ;
19
21
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 } ;
23
23
use rustc_typeck:: hir_ty_to_ty;
24
24
25
25
declare_clippy_lint ! {
@@ -222,13 +222,16 @@ declare_clippy_lint! {
222
222
223
223
#[ derive( Copy , Clone ) ]
224
224
pub struct Functions {
225
- threshold : u64 ,
226
- max_lines : u64 ,
225
+ too_many_arguments_threshold : u64 ,
226
+ too_many_lines_threshold : u64 ,
227
227
}
228
228
229
229
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
+ }
232
235
}
233
236
}
234
237
@@ -252,31 +255,14 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
252
255
span : Span ,
253
256
hir_id : hir:: HirId ,
254
257
) {
258
+ too_many_arguments:: check_fn ( cx, kind, decl, span, hir_id, self . too_many_arguments_threshold ) ;
259
+
255
260
let unsafety = match kind {
256
261
intravisit:: FnKind :: ItemFn ( _, _, hir:: FnHeader { unsafety, .. } , _) => unsafety,
257
262
intravisit:: FnKind :: Method ( _, sig, _) => sig. header . unsafety ,
258
263
intravisit:: FnKind :: Closure => return ,
259
264
} ;
260
265
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
-
280
266
Self :: check_raw_ptr ( cx, unsafety, decl, body, hir_id) ;
281
267
self . check_line_number ( cx, span, body) ;
282
268
}
@@ -335,11 +321,9 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
335
321
}
336
322
337
323
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
+
338
326
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
- }
343
327
let is_public = cx. access_levels . is_exported ( item. hir_id ( ) ) ;
344
328
let fn_header_span = item. span . with_hi ( sig. decl . output . span ( ) . hi ( ) ) ;
345
329
if is_public {
@@ -372,18 +356,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
372
356
}
373
357
374
358
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
-
387
359
fn check_line_number ( self , cx : & LateContext < ' _ > , span : Span , body : & ' tcx hir:: Body < ' _ > ) {
388
360
if in_external_macro ( cx. sess ( ) , span) {
389
361
return ;
@@ -430,12 +402,15 @@ impl<'tcx> Functions {
430
402
}
431
403
}
432
404
433
- if line_count > self . max_lines {
405
+ if line_count > self . too_many_lines_threshold {
434
406
span_lint (
435
407
cx,
436
408
TOO_MANY_LINES ,
437
409
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
+ ) ,
439
414
)
440
415
}
441
416
}
0 commit comments