Skip to content

Commit

Permalink
Make NEEDLESS_PASS_BY_REF_MUT not emitted if `avoid-breaking-exported…
Browse files Browse the repository at this point in the history
…-api` option is enabled
  • Loading branch information
GuillaumeGomez committed Jul 3, 2023
1 parent dd3e00f commit cdfb830
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
6 changes: 5 additions & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
let stack_size_threshold = conf.stack_size_threshold;
store.register_late_pass(move |_| Box::new(large_stack_frames::LargeStackFrames::new(stack_size_threshold)));
store.register_late_pass(|_| Box::new(single_range_in_vec_init::SingleRangeInVecInit));
store.register_late_pass(|_| Box::new(needless_pass_by_ref_mut::NeedlessPassByRefMut));
store.register_late_pass(move |_| {
Box::new(needless_pass_by_ref_mut::NeedlessPassByRefMut::new(
avoid_breaking_exported_api,
))
});
store.register_late_pass(|_| Box::new(incorrect_impls::IncorrectImpls));
store.register_late_pass(move |_| {
Box::new(single_call_fn::SingleCallFn {
Expand Down
22 changes: 20 additions & 2 deletions clippy_lints/src/needless_pass_by_ref_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::{self, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::kw;
use rustc_span::Span;
Expand Down Expand Up @@ -43,7 +43,21 @@ declare_clippy_lint! {
suspicious,
"using a `&mut` argument when it's not mutated"
}
declare_lint_pass!(NeedlessPassByRefMut => [NEEDLESS_PASS_BY_REF_MUT]);

#[derive(Copy, Clone)]
pub struct NeedlessPassByRefMut {
avoid_breaking_exported_api: bool,
}

impl NeedlessPassByRefMut {
pub fn new(avoid_breaking_exported_api: bool) -> Self {
Self {
avoid_breaking_exported_api,
}
}
}

impl_lint_pass!(NeedlessPassByRefMut => [NEEDLESS_PASS_BY_REF_MUT]);

fn should_skip<'tcx>(
cx: &LateContext<'tcx>,
Expand Down Expand Up @@ -81,6 +95,10 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut {
span: Span,
fn_def_id: LocalDefId,
) {
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(fn_def_id) {
return;
}

if span.from_expansion() {
return;
}
Expand Down
10 changes: 1 addition & 9 deletions tests/ui/should_impl_trait/method_list_2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ LL | | }
|
= help: consider implementing the trait `std::hash::Hash` or choosing a less ambiguous method name

error: this argument is a mutable reference, but not used mutably
--> $DIR/method_list_2.rs:38:31
|
LL | pub fn hash(&self, state: &mut T) {
| ^^^^^^ help: consider changing to: `&T`
|
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`

error: method `index` can be confused for the standard trait method `std::ops::Index::index`
--> $DIR/method_list_2.rs:42:5
|
Expand Down Expand Up @@ -157,5 +149,5 @@ LL | | }
|
= help: consider implementing the trait `std::ops::Sub` or choosing a less ambiguous method name

error: aborting due to 16 previous errors
error: aborting due to 15 previous errors

0 comments on commit cdfb830

Please sign in to comment.