Skip to content

Commit dcdd67d

Browse files
Add check_self_items setting for needless_pass_by_ref_mut lint
1 parent 2202493 commit dcdd67d

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

clippy_config/src/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,10 @@ define_Conf! {
589589
/// 2. Paths with any segment that containing the word 'prelude'
590590
/// are already allowed by default.
591591
(allowed_wildcard_imports: FxHashSet<String> = FxHashSet::default()),
592+
/// Lint: NEEDLESS_PASS_BY_REF_MUT.
593+
///
594+
/// Lint on `self`.
595+
(check_self_items: bool = false),
592596
}
593597

594598
/// Search for the configuration file.

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
597597

598598
blacklisted_names: _,
599599
cyclomatic_complexity_threshold: _,
600+
check_self_items,
600601
} = *conf;
601602
let msrv = || msrv.clone();
602603

@@ -1070,6 +1071,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
10701071
store.register_late_pass(move |_| {
10711072
Box::new(needless_pass_by_ref_mut::NeedlessPassByRefMut::new(
10721073
avoid_breaking_exported_api,
1074+
check_self_items,
10731075
))
10741076
});
10751077
store.register_late_pass(|_| Box::new(non_canonical_impls::NonCanonicalImpls));

clippy_lints/src/needless_pass_by_ref_mut.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6266
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 {
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;

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
2525
blacklisted-names
2626
cargo-ignore-publish
2727
check-private-items
28+
check-self-items
2829
cognitive-complexity-threshold
2930
cyclomatic-complexity-threshold
3031
disallowed-macros
@@ -104,6 +105,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
104105
blacklisted-names
105106
cargo-ignore-publish
106107
check-private-items
108+
check-self-items
107109
cognitive-complexity-threshold
108110
cyclomatic-complexity-threshold
109111
disallowed-macros
@@ -183,6 +185,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
183185
blacklisted-names
184186
cargo-ignore-publish
185187
check-private-items
188+
check-self-items
186189
cognitive-complexity-threshold
187190
cyclomatic-complexity-threshold
188191
disallowed-macros

0 commit comments

Comments
 (0)