Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ptr_arg should not warn about &mut Vec<T> for underscore prefixed parameters #13489

Open
cmrschwarz opened this issue Oct 2, 2024 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@cmrschwarz
Copy link

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

@cmrschwarz cmrschwarz added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

1 participant