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

False positive in ptr_arg when function pushes to &mut Vec #8463

Closed
jorgecarleitao opened this issue Feb 23, 2022 · 2 comments · Fixed by #8464
Closed

False positive in ptr_arg when function pushes to &mut Vec #8463

jorgecarleitao opened this issue Feb 23, 2022 · 2 comments · Fixed by #8464
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

@jorgecarleitao
Copy link

Summary

When a function has the signature &mut Vec<T> and pushes to it, it cannot be converted to &mut [T]. However, in some cases, clippy recommends doing so.

Lint Name

ptr_arg

Reproducer

It is not easy to trigger this, so I will use the example I found it:

git clone [email protected]:jorgecarleitao/arrow2.git
cargo clippy --features full

I saw this happen:

warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
   --> src/io/ipc/write/serialize.rs:209:12
    |
209 |     nodes: &mut Vec<ipc::FieldNode>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `&mut [ipc::FieldNode]`
    |
    = note: `#[warn(clippy::ptr_arg)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg

warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
   --> src/io/ipc/write/serialize.rs:298:12
    |
298 |     nodes: &mut Vec<ipc::FieldNode>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `&mut [ipc::FieldNode]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg

warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
   --> src/io/ipc/write/serialize.rs:341:12
    |
341 |     nodes: &mut Vec<ipc::FieldNode>,
    |            ^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `&mut [ipc::FieldNode]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg

warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
   --> src/io/ipc/write/serialize.rs:642:14
    |
642 |     buffers: &mut Vec<ipc::Buffer>,
    |              ^^^^^^^^^^^^^^^^^^^^^ help: change this to: `&mut [ipc::Buffer]`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg

warning: `arrow2` (lib) generated 4 warnings

However, If any of them are changed as suggested, the code does not compile.

The image below quickly illustrates the issue: buffer.push is not implemented for &mut [ipc::Buffer], but clippy recommends replacing it:

Screenshot 2022-02-23 at 05 12 51

Version

rustc 1.61.0-nightly (45e2c2881 2022-02-20)
binary: rustc
commit-hash: 45e2c2881d11324d610815bfff097e25c412199e
commit-date: 2022-02-20
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Additional Labels

No response

@jorgecarleitao jorgecarleitao 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 Feb 23, 2022
@Jarcho Jarcho mentioned this issue Feb 23, 2022
@bors bors closed this as completed in e329249 Feb 26, 2022
@jorgecarleitao
Copy link
Author

@Jarcho, has this been fixed and released? This is now showing in rust stable 1.60.

@Jarcho
Copy link
Contributor

Jarcho commented Apr 12, 2022

I think it just missed the beta branch in February, so it should be in 1.61.

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

Successfully merging a pull request may close this issue.

2 participants