@@ -2,15 +2,17 @@ mod impl_trait_in_params;
2
2
mod misnamed_getters;
3
3
mod must_use;
4
4
mod not_unsafe_ptr_arg_deref;
5
+ mod renamed_function_params;
5
6
mod result;
6
7
mod too_many_arguments;
7
8
mod too_many_lines;
8
9
10
+ use clippy_utils:: def_path_def_ids;
9
11
use rustc_hir as hir;
10
12
use rustc_hir:: intravisit;
11
13
use rustc_lint:: { LateContext , LateLintPass } ;
12
14
use rustc_session:: impl_lint_pass;
13
- use rustc_span:: def_id:: LocalDefId ;
15
+ use rustc_span:: def_id:: { DefIdSet , LocalDefId } ;
14
16
use rustc_span:: Span ;
15
17
16
18
declare_clippy_lint ! {
@@ -359,13 +361,51 @@ declare_clippy_lint! {
359
361
"`impl Trait` is used in the function's parameters"
360
362
}
361
363
362
- #[ derive( Copy , Clone ) ]
363
- #[ allow( clippy:: struct_field_names) ]
364
+ declare_clippy_lint ! {
365
+ /// ### What it does
366
+ /// Lints when the name of function parameters from trait impl is
367
+ /// different than its default implementation.
368
+ ///
369
+ /// ### Why is this bad?
370
+ /// Using the default name for parameters of a trait method is often
371
+ /// more desirable for consistency's sake.
372
+ ///
373
+ /// ### Example
374
+ /// ```rust
375
+ /// struct A(u32);
376
+ ///
377
+ /// impl PartialEq for A {
378
+ /// fn eq(&self, b: &Self) -> bool {
379
+ /// self.0 == b.0
380
+ /// }
381
+ /// }
382
+ /// ```
383
+ /// Use instead:
384
+ /// ```rust
385
+ /// struct A(u32);
386
+ ///
387
+ /// impl PartialEq for A {
388
+ /// fn eq(&self, other: &Self) -> bool {
389
+ /// self.0 == other.0
390
+ /// }
391
+ /// }
392
+ /// ```
393
+ #[ clippy:: version = "1.74.0" ]
394
+ pub RENAMED_FUNCTION_PARAMS ,
395
+ restriction,
396
+ "renamed function parameters in trait implementation"
397
+ }
398
+
399
+ #[ derive( Clone ) ]
364
400
pub struct Functions {
365
401
too_many_arguments_threshold : u64 ,
366
402
too_many_lines_threshold : u64 ,
367
403
large_error_threshold : u64 ,
368
404
avoid_breaking_exported_api : bool ,
405
+ allow_renamed_params_for : Vec < String > ,
406
+ /// A set of resolved `def_id` of traits that are configured to allow
407
+ /// function params renaming.
408
+ trait_ids : DefIdSet ,
369
409
}
370
410
371
411
impl Functions {
@@ -374,12 +414,15 @@ impl Functions {
374
414
too_many_lines_threshold : u64 ,
375
415
large_error_threshold : u64 ,
376
416
avoid_breaking_exported_api : bool ,
417
+ allow_renamed_params_for : Vec < String > ,
377
418
) -> Self {
378
419
Self {
379
420
too_many_arguments_threshold,
380
421
too_many_lines_threshold,
381
422
large_error_threshold,
382
423
avoid_breaking_exported_api,
424
+ allow_renamed_params_for,
425
+ trait_ids : DefIdSet :: default ( ) ,
383
426
}
384
427
}
385
428
}
@@ -395,6 +438,7 @@ impl_lint_pass!(Functions => [
395
438
RESULT_LARGE_ERR ,
396
439
MISNAMED_GETTERS ,
397
440
IMPL_TRAIT_IN_PARAMS ,
441
+ RENAMED_FUNCTION_PARAMS ,
398
442
] ) ;
399
443
400
444
impl < ' tcx > LateLintPass < ' tcx > for Functions {
@@ -424,6 +468,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
424
468
must_use:: check_impl_item ( cx, item) ;
425
469
result:: check_impl_item ( cx, item, self . large_error_threshold ) ;
426
470
impl_trait_in_params:: check_impl_item ( cx, item) ;
471
+ renamed_function_params:: check_impl_item ( cx, item, & self . trait_ids ) ;
427
472
}
428
473
429
474
fn check_trait_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx hir:: TraitItem < ' _ > ) {
@@ -433,4 +478,12 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
433
478
result:: check_trait_item ( cx, item, self . large_error_threshold ) ;
434
479
impl_trait_in_params:: check_trait_item ( cx, item, self . avoid_breaking_exported_api ) ;
435
480
}
481
+
482
+ fn check_crate ( & mut self , cx : & LateContext < ' tcx > ) {
483
+ for path in & self . allow_renamed_params_for {
484
+ let path_segments: Vec < & str > = path. split ( "::" ) . collect ( ) ;
485
+ let ids = def_path_def_ids ( cx, & path_segments) ;
486
+ self . trait_ids . extend ( ids) ;
487
+ }
488
+ }
436
489
}
0 commit comments