Skip to content

Commit cdfb830

Browse files
Make NEEDLESS_PASS_BY_REF_MUT not emitted if avoid-breaking-exported-api option is enabled
1 parent dd3e00f commit cdfb830

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

clippy_lints/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10581058
let stack_size_threshold = conf.stack_size_threshold;
10591059
store.register_late_pass(move |_| Box::new(large_stack_frames::LargeStackFrames::new(stack_size_threshold)));
10601060
store.register_late_pass(|_| Box::new(single_range_in_vec_init::SingleRangeInVecInit));
1061-
store.register_late_pass(|_| Box::new(needless_pass_by_ref_mut::NeedlessPassByRefMut));
1061+
store.register_late_pass(move |_| {
1062+
Box::new(needless_pass_by_ref_mut::NeedlessPassByRefMut::new(
1063+
avoid_breaking_exported_api,
1064+
))
1065+
});
10621066
store.register_late_pass(|_| Box::new(incorrect_impls::IncorrectImpls));
10631067
store.register_late_pass(move |_| {
10641068
Box::new(single_call_fn::SingleCallFn {

clippy_lints/src/needless_pass_by_ref_mut.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_infer::infer::TyCtxtInferExt;
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_middle::mir::FakeReadCause;
1414
use rustc_middle::ty::{self, Ty};
15-
use rustc_session::{declare_lint_pass, declare_tool_lint};
15+
use rustc_session::{declare_tool_lint, impl_lint_pass};
1616
use rustc_span::def_id::LocalDefId;
1717
use rustc_span::symbol::kw;
1818
use rustc_span::Span;
@@ -43,7 +43,21 @@ declare_clippy_lint! {
4343
suspicious,
4444
"using a `&mut` argument when it's not mutated"
4545
}
46-
declare_lint_pass!(NeedlessPassByRefMut => [NEEDLESS_PASS_BY_REF_MUT]);
46+
47+
#[derive(Copy, Clone)]
48+
pub struct NeedlessPassByRefMut {
49+
avoid_breaking_exported_api: bool,
50+
}
51+
52+
impl NeedlessPassByRefMut {
53+
pub fn new(avoid_breaking_exported_api: bool) -> Self {
54+
Self {
55+
avoid_breaking_exported_api,
56+
}
57+
}
58+
}
59+
60+
impl_lint_pass!(NeedlessPassByRefMut => [NEEDLESS_PASS_BY_REF_MUT]);
4761

4862
fn should_skip<'tcx>(
4963
cx: &LateContext<'tcx>,
@@ -81,6 +95,10 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut {
8195
span: Span,
8296
fn_def_id: LocalDefId,
8397
) {
98+
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(fn_def_id) {
99+
return;
100+
}
101+
84102
if span.from_expansion() {
85103
return;
86104
}

tests/ui/should_impl_trait/method_list_2.stderr

+1-9
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ LL | | }
3939
|
4040
= help: consider implementing the trait `std::hash::Hash` or choosing a less ambiguous method name
4141

42-
error: this argument is a mutable reference, but not used mutably
43-
--> $DIR/method_list_2.rs:38:31
44-
|
45-
LL | pub fn hash(&self, state: &mut T) {
46-
| ^^^^^^ help: consider changing to: `&T`
47-
|
48-
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
49-
5042
error: method `index` can be confused for the standard trait method `std::ops::Index::index`
5143
--> $DIR/method_list_2.rs:42:5
5244
|
@@ -157,5 +149,5 @@ LL | | }
157149
|
158150
= help: consider implementing the trait `std::ops::Sub` or choosing a less ambiguous method name
159151

160-
error: aborting due to 16 previous errors
152+
error: aborting due to 15 previous errors
161153

0 commit comments

Comments
 (0)