Skip to content

Commit 46de00f

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 929a288 commit 46de00f

8 files changed

+58
-32
lines changed

clippy_lints/src/needless_pass_by_ref_mut.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
229229
.iter()
230230
.filter(|(def_id, _)| !self.used_fn_def_ids.contains(def_id))
231231
{
232-
let show_semver_warning =
233-
self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(*fn_def_id);
232+
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(*fn_def_id) {
233+
continue;
234+
}
234235

235236
let mut is_cfged = None;
236237
for input in unused {
@@ -251,9 +252,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByRefMut<'tcx> {
251252
format!("&{}", snippet(cx, cx.tcx.hir().span(inner_ty.ty.hir_id), "_"),),
252253
Applicability::Unspecified,
253254
);
254-
if show_semver_warning {
255-
diag.warn("changing this function will impact semver compatibility");
256-
}
257255
if *is_cfged {
258256
diag.note("this is cfg-gated and may require further changes");
259257
}

clippy_lints/src/utils/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ define_Conf! {
290290
/// arithmetic-side-effects-allowed-unary = ["SomeType", "AnotherType"]
291291
/// ```
292292
(arithmetic_side_effects_allowed_unary: rustc_data_structures::fx::FxHashSet<String> = <_>::default()),
293-
/// 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.
293+
/// 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.
294294
///
295295
/// Suppress lints whenever the suggested change would cause breakage for other crates.
296296
(avoid_breaking_exported_api: bool = true),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
avoid-breaking-exported-api = false
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() {}
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() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
= note: `-D clippy::needless-pass-by-ref-mut` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::needless_pass_by_ref_mut)]`
9+
10+
error: aborting due to previous error
11+

tests/ui/needless_pass_by_ref_mut.rs

+12-6
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
fn main() {
274279
let mut u = 0;
275280
let mut v = vec![0];
@@ -286,4 +291,5 @@ fn main() {
286291
used_as_path;
287292
let _: fn(&mut u32) = passed_as_local;
288293
let _ = if v[0] == 0 { ty_unify_1 } else { ty_unify_2 };
294+
pub_foo(&mut v, &0, &mut u);
289295
}

tests/ui/needless_pass_by_ref_mut.stderr

+12-20
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)