Skip to content

Commit 63b69fe

Browse files
committed
Honor avoid-breaking-exported-api in needless_pass_by_ref_mut
Until now, the lint only emitted a warning, when breaking public API. Now it doesn't lint at all when the config value is not set to `false`, bringing it in line with the other lints using this config value. Also ensures that this config value is documented in the lint.
1 parent 2b030eb commit 63b69fe

File tree

8 files changed

+62
-31
lines changed

8 files changed

+62
-31
lines changed

clippy_lints/src/needless_pass_by_ref_mut.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ declare_clippy_lint! {
3030
/// Check if a `&mut` function argument is actually used mutably.
3131
///
3232
/// Be careful if the function is publicly reexported as it would break compatibility with
33-
/// users of this function.
33+
/// users of this function, when the users pass this function as an argument.
3434
///
3535
/// ### Why is this bad?
3636
/// Less `mut` means less fights with the borrow checker. It can also lead to more
@@ -241,8 +241,10 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
241241
.iter()
242242
.filter(|(def_id, _)| !self.used_fn_def_ids.contains(def_id))
243243
{
244-
let show_semver_warning =
245-
self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(*fn_def_id);
244+
let is_exported = cx.effective_visibilities.is_exported(*fn_def_id);
245+
if self.avoid_breaking_exported_api && is_exported {
246+
continue;
247+
}
246248

247249
let mut is_cfged = None;
248250
for input in unused {
@@ -263,7 +265,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
263265
format!("&{}", snippet(cx, cx.tcx.hir().span(inner_ty.ty.hir_id), "_"),),
264266
Applicability::Unspecified,
265267
);
266-
if show_semver_warning {
268+
if is_exported {
267269
diag.warn("changing this function will impact semver compatibility");
268270
}
269271
if *is_cfged {

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ define_Conf! {
264264
/// arithmetic-side-effects-allowed-unary = ["SomeType", "AnotherType"]
265265
/// ```
266266
(arithmetic_side_effects_allowed_unary: rustc_data_structures::fx::FxHashSet<String> = <_>::default()),
267-
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UNUSED_SELF, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_COLLECTION, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX, UNNECESSARY_BOX_RETURNS, SINGLE_CALL_FN.
267+
/// Lint: ENUM_VARIANT_NAMES, LARGE_TYPES_PASSED_BY_VALUE, TRIVIALLY_COPY_PASS_BY_REF, UNNECESSARY_WRAPS, UNUSED_SELF, UPPER_CASE_ACRONYMS, WRONG_SELF_CONVENTION, BOX_COLLECTION, REDUNDANT_ALLOCATION, RC_BUFFER, VEC_BOX, OPTION_OPTION, LINKEDLIST, RC_MUTEX, UNNECESSARY_BOX_RETURNS, SINGLE_CALL_FN, NEEDLESS_PASS_BY_REF_MUT.
268268
///
269269
/// Suppress lints whenever the suggested change would cause breakage for other crates.
270270
(avoid_breaking_exported_api: bool = true),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
avoid-breaking-exported-api = false
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![warn(clippy::needless_pass_by_ref_mut)]
2+
3+
// Should warn
4+
pub fn pub_foo(s: &Vec<u32>, b: &u32, x: &mut u32) {
5+
//~^ ERROR: this argument is a mutable reference, but not used mutably
6+
*x += *b + s.len() as u32;
7+
}
8+
9+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![warn(clippy::needless_pass_by_ref_mut)]
2+
3+
// Should warn
4+
pub fn pub_foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) {
5+
//~^ ERROR: this argument is a mutable reference, but not used mutably
6+
*x += *b + s.len() as u32;
7+
}
8+
9+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: this argument is a mutable reference, but not used mutably
2+
--> $DIR/needless_pass_by_ref_mut.rs:4:19
3+
|
4+
LL | pub fn pub_foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) {
5+
| ^^^^^^^^^^^^^ help: consider changing to: `&Vec<u32>`
6+
|
7+
= warning: changing this function will impact semver compatibility
8+
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::needless_pass_by_ref_mut)]`
10+
11+
error: aborting due to previous error
12+

tests/ui/needless_pass_by_ref_mut.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,43 +233,48 @@ async fn async_vec2(b: &mut Vec<bool>) {
233233
}
234234
fn non_mut(n: &str) {}
235235
//Should warn
236-
pub async fn call_in_closure1(n: &mut str) {
236+
async fn call_in_closure1(n: &mut str) {
237237
(|| non_mut(n))()
238238
}
239239
fn str_mut(str: &mut String) -> bool {
240240
str.pop().is_some()
241241
}
242242
//Should not warn
243-
pub async fn call_in_closure2(str: &mut String) {
243+
async fn call_in_closure2(str: &mut String) {
244244
(|| str_mut(str))();
245245
}
246246

247247
// Should not warn.
248-
pub async fn closure(n: &mut usize) -> impl '_ + FnMut() {
248+
async fn closure(n: &mut usize) -> impl '_ + FnMut() {
249249
|| {
250250
*n += 1;
251251
}
252252
}
253253

254254
// Should warn.
255-
pub fn closure2(n: &mut usize) -> impl '_ + FnMut() -> usize {
255+
fn closure2(n: &mut usize) -> impl '_ + FnMut() -> usize {
256256
//~^ ERROR: this argument is a mutable reference, but not used mutably
257257
|| *n + 1
258258
}
259259

260260
// Should not warn.
261-
pub async fn closure3(n: &mut usize) {
261+
async fn closure3(n: &mut usize) {
262262
(|| *n += 1)();
263263
}
264264

265265
// Should warn.
266-
pub async fn closure4(n: &mut usize) {
266+
async fn closure4(n: &mut usize) {
267267
//~^ ERROR: this argument is a mutable reference, but not used mutably
268268
(|| {
269269
let _x = *n + 1;
270270
})();
271271
}
272272

273+
// Should not warn: pub
274+
pub fn pub_foo(s: &mut Vec<u32>, b: &u32, x: &mut u32) {
275+
*x += *b + s.len() as u32;
276+
}
277+
273278
// Should not warn.
274279
async fn _f(v: &mut Vec<()>) {
275280
let x = || v.pop();
@@ -318,4 +323,5 @@ fn main() {
318323
used_as_path;
319324
let _: fn(&mut u32) = passed_as_local;
320325
let _ = if v[0] == 0 { ty_unify_1 } else { ty_unify_2 };
326+
pub_foo(&mut v, &0, &mut u);
321327
}

tests/ui/needless_pass_by_ref_mut.stderr

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,36 +108,28 @@ LL | async fn inner_async3(x: &mut i32, y: &mut u32) {
108108
| ^^^^^^^^ help: consider changing to: `&i32`
109109

110110
error: this argument is a mutable reference, but not used mutably
111-
--> $DIR/needless_pass_by_ref_mut.rs:236:34
111+
--> $DIR/needless_pass_by_ref_mut.rs:236:30
112112
|
113-
LL | pub async fn call_in_closure1(n: &mut str) {
114-
| ^^^^^^^^ help: consider changing to: `&str`
115-
|
116-
= warning: changing this function will impact semver compatibility
113+
LL | async fn call_in_closure1(n: &mut str) {
114+
| ^^^^^^^^ help: consider changing to: `&str`
117115

118116
error: this argument is a mutable reference, but not used mutably
119-
--> $DIR/needless_pass_by_ref_mut.rs:248:25
120-
|
121-
LL | pub async fn closure(n: &mut usize) -> impl '_ + FnMut() {
122-
| ^^^^^^^^^^ help: consider changing to: `&usize`
117+
--> $DIR/needless_pass_by_ref_mut.rs:248:21
123118
|
124-
= warning: changing this function will impact semver compatibility
119+
LL | async fn closure(n: &mut usize) -> impl '_ + FnMut() {
120+
| ^^^^^^^^^^ help: consider changing to: `&usize`
125121

126122
error: this argument is a mutable reference, but not used mutably
127-
--> $DIR/needless_pass_by_ref_mut.rs:255:20
123+
--> $DIR/needless_pass_by_ref_mut.rs:255:16
128124
|
129-
LL | pub fn closure2(n: &mut usize) -> impl '_ + FnMut() -> usize {
130-
| ^^^^^^^^^^ help: consider changing to: `&usize`
131-
|
132-
= warning: changing this function will impact semver compatibility
125+
LL | fn closure2(n: &mut usize) -> impl '_ + FnMut() -> usize {
126+
| ^^^^^^^^^^ help: consider changing to: `&usize`
133127

134128
error: this argument is a mutable reference, but not used mutably
135-
--> $DIR/needless_pass_by_ref_mut.rs:266:26
136-
|
137-
LL | pub async fn closure4(n: &mut usize) {
138-
| ^^^^^^^^^^ help: consider changing to: `&usize`
129+
--> $DIR/needless_pass_by_ref_mut.rs:266:22
139130
|
140-
= warning: changing this function will impact semver compatibility
131+
LL | async fn closure4(n: &mut usize) {
132+
| ^^^^^^^^^^ help: consider changing to: `&usize`
141133

142134
error: aborting due to 21 previous errors
143135

0 commit comments

Comments
 (0)