@@ -2,15 +2,17 @@ mod impl_trait_in_params;
22mod misnamed_getters;
33mod must_use;
44mod not_unsafe_ptr_arg_deref;
5+ mod renamed_function_params;
56mod result;
67mod too_many_arguments;
78mod too_many_lines;
89
10+ use clippy_utils:: def_path_def_ids;
911use rustc_hir as hir;
1012use rustc_hir:: intravisit;
1113use rustc_lint:: { LateContext , LateLintPass } ;
1214use rustc_session:: impl_lint_pass;
13- use rustc_span:: def_id:: LocalDefId ;
15+ use rustc_span:: def_id:: { DefIdSet , LocalDefId } ;
1416use rustc_span:: Span ;
1517
1618declare_clippy_lint ! {
@@ -359,13 +361,51 @@ declare_clippy_lint! {
359361 "`impl Trait` is used in the function's parameters"
360362}
361363
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 ) ]
364400pub struct Functions {
365401 too_many_arguments_threshold : u64 ,
366402 too_many_lines_threshold : u64 ,
367403 large_error_threshold : u64 ,
368404 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 ,
369409}
370410
371411impl Functions {
@@ -374,12 +414,15 @@ impl Functions {
374414 too_many_lines_threshold : u64 ,
375415 large_error_threshold : u64 ,
376416 avoid_breaking_exported_api : bool ,
417+ allow_renamed_params_for : Vec < String > ,
377418 ) -> Self {
378419 Self {
379420 too_many_arguments_threshold,
380421 too_many_lines_threshold,
381422 large_error_threshold,
382423 avoid_breaking_exported_api,
424+ allow_renamed_params_for,
425+ trait_ids : DefIdSet :: default ( ) ,
383426 }
384427 }
385428}
@@ -395,6 +438,7 @@ impl_lint_pass!(Functions => [
395438 RESULT_LARGE_ERR ,
396439 MISNAMED_GETTERS ,
397440 IMPL_TRAIT_IN_PARAMS ,
441+ RENAMED_FUNCTION_PARAMS ,
398442] ) ;
399443
400444impl < ' tcx > LateLintPass < ' tcx > for Functions {
@@ -424,6 +468,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
424468 must_use:: check_impl_item ( cx, item) ;
425469 result:: check_impl_item ( cx, item, self . large_error_threshold ) ;
426470 impl_trait_in_params:: check_impl_item ( cx, item) ;
471+ renamed_function_params:: check_impl_item ( cx, item, & self . trait_ids ) ;
427472 }
428473
429474 fn check_trait_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx hir:: TraitItem < ' _ > ) {
@@ -433,4 +478,12 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
433478 result:: check_trait_item ( cx, item, self . large_error_threshold ) ;
434479 impl_trait_in_params:: check_trait_item ( cx, item, self . avoid_breaking_exported_api ) ;
435480 }
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+ }
436489}
0 commit comments