@@ -30,6 +30,9 @@ declare_clippy_lint! {
30
30
/// Be careful if the function is publicly reexported as it would break compatibility with
31
31
/// users of this function.
32
32
///
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
+ ///
33
36
/// ### Why is this bad?
34
37
/// Less `mut` means less fights with the borrow checker. It can also lead to more
35
38
/// opportunities for parallelization.
@@ -57,14 +60,16 @@ pub struct NeedlessPassByRefMut<'tcx> {
57
60
avoid_breaking_exported_api : bool ,
58
61
used_fn_def_ids : FxHashSet < LocalDefId > ,
59
62
fn_def_ids_to_maybe_unused_mut : FxIndexMap < LocalDefId , Vec < rustc_hir:: Ty < ' tcx > > > ,
63
+ check_self_items : bool ,
60
64
}
61
65
62
66
impl 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 {
64
68
Self {
65
69
avoid_breaking_exported_api,
66
70
used_fn_def_ids : FxHashSet :: default ( ) ,
67
71
fn_def_ids_to_maybe_unused_mut : FxIndexMap :: default ( ) ,
72
+ check_self_items,
68
73
}
69
74
}
70
75
}
@@ -76,13 +81,14 @@ fn should_skip<'tcx>(
76
81
input : rustc_hir:: Ty < ' tcx > ,
77
82
ty : Ty < ' _ > ,
78
83
arg : & rustc_hir:: Param < ' _ > ,
84
+ check_self_items : bool ,
79
85
) -> bool {
80
86
// We check if this a `&mut`. `ref_mutability` returns `None` if it's not a reference.
81
87
if !matches ! ( ty. ref_mutability( ) , Some ( Mutability :: Mut ) ) {
82
88
return true ;
83
89
}
84
90
85
- if is_self ( arg) {
91
+ if !check_self_items && is_self ( arg) {
86
92
return true ;
87
93
}
88
94
@@ -172,13 +178,15 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
172
178
let fn_sig = cx. tcx . fn_sig ( fn_def_id) . instantiate_identity ( ) ;
173
179
let fn_sig = cx. tcx . liberate_late_bound_regions ( fn_def_id. to_def_id ( ) , fn_sig) ;
174
180
181
+ let check_self_items = self . check_self_items ;
182
+
175
183
// If there are no `&mut` argument, no need to go any further.
176
184
let mut it = decl
177
185
. inputs
178
186
. iter ( )
179
187
. zip ( fn_sig. inputs ( ) )
180
188
. 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 ) )
182
190
. peekable ( ) ;
183
191
if it. peek ( ) . is_none ( ) {
184
192
return ;
0 commit comments