rust-clippy
rust-clippy copied to clipboard
`ptr_arg` should not warn about &mut Vec<T> for underscore prefixed parameters
Summary
Clippy only warns about parameters of type &mut Vec<T> being replaceable by &mut [T] if none of the Vec APIs are used.
For example, you can't use &mut [T] instead if you want to push into the Vec.
A leading _ indicates intentionally unused parameters. The compiler should essentially assume (for the purpose of warnings) that the parameter is being used, but in a way that it cannot see. This is usually done for todo!s, #[cfg] stuff or api requirements.
If there's a usage that we cannot see, than we should not assume anything about that usage. So we should not assume that it isn't e.g. a push into the Vec.
Therefore, the warning about &mut Vec<T> should not be emitted for such parameters.
Lint Name
clippy::ptr_arg
Reproducer
I tried this code:
fn foo(_x: &mut Vec<i32>) {
todo!();
}
I saw this happen:
warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
--> src/main.rs:1:12
|
1 | fn foo(_x: &mut Vec<i32>) {
| ^^^^^^^^^^^^^ help: change this to: `&mut [i32]`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg
= note: `#[warn(clippy::ptr_arg)]` on by default
I expected to see this happen: No diagnostic.
Version
rustc 1.81.0 (eeb90cda1 2024-09-04) binary: rustc commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c commit-date: 2024-09-04 host: x86_64-unknown-linux-gnu release: 1.81.0 LLVM version: 18.1.7
Additional Labels
No response