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.
This commit is contained in:
Yuri Astrakhan 2024-07-16 17:45:22 -04:00
parent 0ee9f44568
commit 197b444407
3 changed files with 21 additions and 16 deletions

View file

@ -129,7 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Collect moved variables and spans which will need dereferencings from the // Collect moved variables and spans which will need dereferencing from the
// function body. // function body.
let MovedVariablesCtxt { moved_vars } = { let MovedVariablesCtxt { moved_vars } = {
let mut ctx = MovedVariablesCtxt::default(); let mut ctx = MovedVariablesCtxt::default();
@ -148,12 +148,13 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
return; return;
} }
// Ignore `self`s. // Ignore `self`s and params whose variable name starts with an underscore
if idx == 0 { if let PatKind::Binding(.., ident, _) = arg.pat.kind {
if let PatKind::Binding(.., ident, _) = arg.pat.kind { if idx == 0 && ident.name == kw::SelfLower {
if ident.name == kw::SelfLower { continue;
continue; }
} if ident.name.as_str().starts_with('_') {
continue;
} }
} }

View file

@ -84,7 +84,7 @@ trait Serialize {}
impl<'a, T> Serialize for &'a T where T: Serialize {} impl<'a, T> Serialize for &'a T where T: Serialize {}
impl Serialize for i32 {} impl Serialize for i32 {}
fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {} fn test_blanket_ref<T: Foo, S: Serialize>(vals: T, serializable: S) {}
//~^ ERROR: this argument is passed by value, but not consumed in the function body //~^ ERROR: this argument is passed by value, but not consumed in the function body
fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) { fn issue_2114(s: String, t: String, u: Vec<i32>, v: Vec<i32>) {
@ -116,7 +116,7 @@ impl<T: Serialize, U> S<T, U> {
) { ) {
} }
fn baz(&self, _u: U, _s: Self) {} fn baz(&self, uu: U, ss: Self) {}
//~^ ERROR: this argument is passed by value, but not consumed in the function body //~^ ERROR: this argument is passed by value, but not consumed in the function body
//~| ERROR: this argument is passed by value, but not consumed in the function body //~| ERROR: this argument is passed by value, but not consumed in the function body
} }
@ -162,13 +162,13 @@ fn test_destructure_copy(x: CopyWrapper, y: CopyWrapper, z: CopyWrapper) {
// The following 3 lines should not cause an ICE. See #2831 // The following 3 lines should not cause an ICE. See #2831
trait Bar<'a, A> {} trait Bar<'a, A> {}
impl<'b, T> Bar<'b, T> for T {} impl<'b, T> Bar<'b, T> for T {}
fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {} fn some_fun<'b, S: Bar<'b, ()>>(items: S) {}
//~^ ERROR: this argument is passed by value, but not consumed in the function body //~^ ERROR: this argument is passed by value, but not consumed in the function body
// Also this should not cause an ICE. See #2831 // Also this should not cause an ICE. See #2831
trait Club<'a, A> {} trait Club<'a, A> {}
impl<T> Club<'static, T> for T {} impl<T> Club<'static, T> for T {}
fn more_fun(_item: impl Club<'static, i32>) {} fn more_fun(items: impl Club<'static, i32>) {}
//~^ ERROR: this argument is passed by value, but not consumed in the function body //~^ ERROR: this argument is passed by value, but not consumed in the function body
fn is_sync<T>(_: T) fn is_sync<T>(_: T)
@ -177,6 +177,10 @@ where
{ {
} }
struct Obj(String);
fn prefix_test(_unused_with_prefix: Obj) {}
fn main() { fn main() {
// This should not cause an ICE either // This should not cause an ICE either
// https://github.com/rust-lang/rust-clippy/issues/3144 // https://github.com/rust-lang/rust-clippy/issues/3144

View file

@ -46,7 +46,7 @@ LL | fn test_destructure(x: Wrapper, y: Wrapper, z: Wrapper) {
error: this argument is passed by value, but not consumed in the function body error: this argument is passed by value, but not consumed in the function body
--> tests/ui/needless_pass_by_value.rs:87:49 --> tests/ui/needless_pass_by_value.rs:87:49
| |
LL | fn test_blanket_ref<T: Foo, S: Serialize>(_foo: T, _serializable: S) {} LL | fn test_blanket_ref<T: Foo, S: Serialize>(vals: T, serializable: S) {}
| ^ help: consider taking a reference instead: `&T` | ^ help: consider taking a reference instead: `&T`
error: this argument is passed by value, but not consumed in the function body error: this argument is passed by value, but not consumed in the function body
@ -106,13 +106,13 @@ LL | t: String,
error: this argument is passed by value, but not consumed in the function body error: this argument is passed by value, but not consumed in the function body
--> tests/ui/needless_pass_by_value.rs:119:23 --> tests/ui/needless_pass_by_value.rs:119:23
| |
LL | fn baz(&self, _u: U, _s: Self) {} LL | fn baz(&self, uu: U, ss: Self) {}
| ^ help: consider taking a reference instead: `&U` | ^ help: consider taking a reference instead: `&U`
error: this argument is passed by value, but not consumed in the function body error: this argument is passed by value, but not consumed in the function body
--> tests/ui/needless_pass_by_value.rs:119:30 --> tests/ui/needless_pass_by_value.rs:119:30
| |
LL | fn baz(&self, _u: U, _s: Self) {} LL | fn baz(&self, uu: U, ss: Self) {}
| ^^^^ help: consider taking a reference instead: `&Self` | ^^^^ help: consider taking a reference instead: `&Self`
error: this argument is passed by value, but not consumed in the function body error: this argument is passed by value, but not consumed in the function body
@ -166,13 +166,13 @@ LL | struct CopyWrapper(u32);
error: this argument is passed by value, but not consumed in the function body error: this argument is passed by value, but not consumed in the function body
--> tests/ui/needless_pass_by_value.rs:165:40 --> tests/ui/needless_pass_by_value.rs:165:40
| |
LL | fn some_fun<'b, S: Bar<'b, ()>>(_item: S) {} LL | fn some_fun<'b, S: Bar<'b, ()>>(items: S) {}
| ^ help: consider taking a reference instead: `&S` | ^ help: consider taking a reference instead: `&S`
error: this argument is passed by value, but not consumed in the function body error: this argument is passed by value, but not consumed in the function body
--> tests/ui/needless_pass_by_value.rs:171:20 --> tests/ui/needless_pass_by_value.rs:171:20
| |
LL | fn more_fun(_item: impl Club<'static, i32>) {} LL | fn more_fun(items: impl Club<'static, i32>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>` | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider taking a reference instead: `&impl Club<'static, i32>`
error: aborting due to 22 previous errors error: aborting due to 22 previous errors