@@ -30,6 +30,9 @@ declare_clippy_lint! {
3030 /// Be careful if the function is publicly reexported as it would break compatibility with
3131 /// users of this function.
3232 ///
33+ /// By default, `&mut self` is ignored. If you want it to be taken into account, set the
34+ /// `check_self_items` clippy setting to `true`.
35+ ///
3336 /// ### Why is this bad?
3437 /// Less `mut` means less fights with the borrow checker. It can also lead to more
3538 /// opportunities for parallelization.
@@ -57,14 +60,16 @@ pub struct NeedlessPassByRefMut<'tcx> {
5760 avoid_breaking_exported_api : bool ,
5861 used_fn_def_ids : FxHashSet < LocalDefId > ,
5962 fn_def_ids_to_maybe_unused_mut : FxIndexMap < LocalDefId , Vec < rustc_hir:: Ty < ' tcx > > > ,
63+ check_self_items : bool ,
6064}
6165
6266impl NeedlessPassByRefMut < ' _ > {
63- pub fn new ( avoid_breaking_exported_api : bool ) -> Self {
67+ pub fn new ( avoid_breaking_exported_api : bool , check_self_items : bool ) -> Self {
6468 Self {
6569 avoid_breaking_exported_api,
6670 used_fn_def_ids : FxHashSet :: default ( ) ,
6771 fn_def_ids_to_maybe_unused_mut : FxIndexMap :: default ( ) ,
72+ check_self_items,
6873 }
6974 }
7075}
@@ -76,13 +81,14 @@ fn should_skip<'tcx>(
7681 input : rustc_hir:: Ty < ' tcx > ,
7782 ty : Ty < ' _ > ,
7883 arg : & rustc_hir:: Param < ' _ > ,
84+ check_self_items : bool ,
7985) -> bool {
8086 // We check if this a `&mut`. `ref_mutability` returns `None` if it's not a reference.
8187 if !matches ! ( ty. ref_mutability( ) , Some ( Mutability :: Mut ) ) {
8288 return true ;
8389 }
8490
85- if is_self ( arg) {
91+ if !check_self_items && is_self ( arg) {
8692 return true ;
8793 }
8894
@@ -172,13 +178,15 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
172178 let fn_sig = cx. tcx . fn_sig ( fn_def_id) . instantiate_identity ( ) ;
173179 let fn_sig = cx. tcx . liberate_late_bound_regions ( fn_def_id. to_def_id ( ) , fn_sig) ;
174180
181+ let check_self_items = self . check_self_items ;
182+
175183 // If there are no `&mut` argument, no need to go any further.
176184 let mut it = decl
177185 . inputs
178186 . iter ( )
179187 . zip ( fn_sig. inputs ( ) )
180188 . zip ( body. params )
181- . filter ( |( ( & input, & ty) , arg) | !should_skip ( cx, input, ty, arg) )
189+ . filter ( |( ( & input, & ty) , arg) | !should_skip ( cx, input, ty, arg, check_self_items ) )
182190 . peekable ( ) ;
183191 if it. peek ( ) . is_none ( ) {
184192 return ;
0 commit comments