Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 083e20a

Browse files
committed
Auto merge of rust-lang#13113 - nyurik:ignore-pass-by-val-for-pfx, r=blyxyas
Ignore underscore-prefixed args for needless_pass_by_value lint When a user explicitly tags a param as unused (yet?), there is no need to raise another lint on it. fixes rust-lang#7295 Note that I had to rename all `_*` params in the tests, but kept the variable name length to avoid extra changes in the expected output. changelog: [`needless_pass_by_value`]: do not warn if the argument name starts with an `_`
2 parents 40bca0d + 197b444 commit 083e20a

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
129129
})
130130
.collect::<Vec<_>>();
131131

132-
// Collect moved variables and spans which will need dereferencings from the
132+
// Collect moved variables and spans which will need dereferencing from the
133133
// function body.
134134
let MovedVariablesCtxt { moved_vars } = {
135135
let mut ctx = MovedVariablesCtxt::default();
@@ -148,12 +148,13 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
148148
return;
149149
}
150150

151-
// Ignore `self`s.
152-
if idx == 0 {
153-
if let PatKind::Binding(.., ident, _) = arg.pat.kind {
154-
if ident.name == kw::SelfLower {
155-
continue;
156-
}
151+
// Ignore `self`s and params whose variable name starts with an underscore
152+
if let PatKind::Binding(.., ident, _) = arg.pat.kind {
153+
if idx == 0 && ident.name == kw::SelfLower {
154+
continue;
155+
}
156+
if ident.name.as_str().starts_with('_') {
157+
continue;
157158
}
158159
}
159160

tests/ui/needless_pass_by_value.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ trait Serialize {}
8484
impl<'a, T> Serialize for &'a T where T: Serialize {}
8585
impl Serialize for i32 {}
8686

87-
fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
87+
fn test_blanket_ref<T: Foo, S: Serialize>(vals: T, serializable: S) {}
8888
//~^ ERROR: this argument is passed by value, but not consumed in the function body
8989

9090
fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
@@ -116,7 +116,7 @@ impl<T: Serialize, U> S<T, U> {
116116
) {
117117
}
118118

119-
fn baz(&self, _u: U, _s: Self) {}
119+
fn baz(&self, uu: U, ss: Self) {}
120120
//~^ ERROR: this argument is passed by value, but not consumed in the function body
121121
//~| ERROR: this argument is passed by value, but not consumed in the function body
122122
}
@@ -162,13 +162,13 @@ fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
162162
// The following 3 lines should not cause an ICE. See #2831
163163
trait Bar<'a, A> {}
164164
impl<'b, T> Bar<'b, T> for T {}
165-
fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
165+
fn some_fun<'b, S: Bar<'b, ()>>(items: S) {}
166166
//~^ ERROR: this argument is passed by value, but not consumed in the function body
167167

168168
// Also this should not cause an ICE. See #2831
169169
trait Club<'a, A> {}
170170
impl<T> Club<'static, T> for T {}
171-
fn more_fun(_item: impl Club<'static, i32>) {}
171+
fn more_fun(items: impl Club<'static, i32>) {}
172172
//~^ ERROR: this argument is passed by value, but not consumed in the function body
173173

174174
fn is_sync<T>(_: T)
@@ -177,6 +177,10 @@ where
177177
{
178178
}
179179

180+
struct Obj(String);
181+
182+
fn prefix_test(_unused_with_prefix: Obj) {}
183+
180184
fn main() {
181185
// This should not cause an ICE either
182186
// https://github.com/rust-lang/rust-clippy/issues/3144

tests/ui/needless_pass_by_value.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ LL | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
4646
error: this argument is passed by value, but not consumed in the function body
4747
--> tests/ui/needless_pass_by_value.rs:87:49
4848
|
49-
LL | fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {}
49+
LL | fn test_blanket_ref<T: Foo, S: Serialize>(vals: T, serializable: S) {}
5050
| ^ help: consider taking a reference instead: `&T`
5151

5252
error: this argument is passed by value, but not consumed in the function body
@@ -106,13 +106,13 @@ LL | t: String,
106106
error: this argument is passed by value, but not consumed in the function body
107107
--> tests/ui/needless_pass_by_value.rs:119:23
108108
|
109-
LL | fn baz(&self, _u: U, _s: Self) {}
109+
LL | fn baz(&self, uu: U, ss: Self) {}
110110
| ^ help: consider taking a reference instead: `&U`
111111

112112
error: this argument is passed by value, but not consumed in the function body
113113
--> tests/ui/needless_pass_by_value.rs:119:30
114114
|
115-
LL | fn baz(&self, _u: U, _s: Self) {}
115+
LL | fn baz(&self, uu: U, ss: Self) {}
116116
| ^^^^ help: consider taking a reference instead: `&Self`
117117

118118
error: this argument is passed by value, but not consumed in the function body
@@ -166,13 +166,13 @@ LL | struct CopyWrapper(u32);
166166
error: this argument is passed by value, but not consumed in the function body
167167
--> tests/ui/needless_pass_by_value.rs:165:40
168168
|
169-
LL | fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {}
169+
LL | fn some_fun<'b, S: Bar<'b, ()>>(items: S) {}
170170
| ^ help: consider taking a reference instead: `&S`
171171

172172
error: this argument is passed by value, but not consumed in the function body
173173
--> tests/ui/needless_pass_by_value.rs:171:20
174174
|
175-
LL | fn more_fun(_item: impl Club<'static, i32>) {}
175+
LL | fn more_fun(items: impl Club<'static, i32>) {}
176176
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>`
177177

178178
error: aborting due to 22 previous errors

0 commit comments

Comments
 (0)