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

Suggests replacing &mut Vec with &mut [] (ptr_arg) #8388

Closed
ijackson opened this issue Feb 2, 2022 · 10 comments
Closed

Suggests replacing &mut Vec with &mut [] (ptr_arg) #8388

ijackson opened this issue Feb 2, 2022 · 10 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ijackson
Copy link

ijackson commented Feb 2, 2022

Summary

ptr_arg ought only to trigger on &Vec, not &mut Vec. In general, if someone passes &mut SomeObject, they are likely to be meaning to give access to its methods.

Current stable seems to work fine.

Reproducer

I tried this code:

pub trait Body: Sized {
    fn encode_onto(self, w: &mut Vec<u8>);
}

struct Wombat;

impl Body for Wombat {
    fn encode_onto(self, w: &mut Vec<u8>) {
        w.push(0);
    }
}

fn main() {
    let mut w = vec![42];
    Wombat.encode_onto(&mut w);
    println!("{:?}", &w);
}

I expected to see this happen:

No complaints.

Instead, this happened:

nailing-cargo: invoking: cargo clippy --manifest-path=/home/ian/Rustup/Arti/experiments/Cargo.toml --locked --target-dir=target --offline
    Checking foo v0.1.0 (/home/ian/Rustup/Arti/experiments)
warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
 --> src/main.rs:3:29
  |
3 |     fn encode_onto(self, w: &mut Vec<u8>);
  |                             ^^^^^^^^^^^^ help: change this to: `&mut [u8]`
  |
  = 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: `foo` (bin "foo") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
nailing-cargo: unnailed.  status 0.

Compilation finished at Wed Feb  2 18:01:56

Needless to say, the suggestion doesn't compile.

Version

rustcargo@zealot:/home/ian/Rustup/Arti/experiments$ rustc -Vv
rustc 1.60.0-nightly (a00e130da 2022-01-29)
binary: rustc
commit-hash: a00e130dae74a213338e2b095ec855156d8f3d8a
commit-date: 2022-01-29
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0
rustcargo@zealot:/home/ian/Rustup/Arti/experiments$

Additional Labels

@rustbot label +I-suggestion-causes-error

@ijackson ijackson added the C-bug Category: Clippy is not doing the correct thing label Feb 2, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Feb 2, 2022
@ijackson
Copy link
Author

ijackson commented Feb 2, 2022

@rustbot modify labels +regressoin-from-stable-to-nightly

@rustbot

This comment has been minimized.

@ijackson
Copy link
Author

ijackson commented Feb 2, 2022

@rustbod modify labels +regession-from-stable-to-nightly

@ijackson
Copy link
Author

ijackson commented Feb 2, 2022

@rustbot modify labels +regession-from-stable-to-nightly

this time for sure

@rustbot

This comment has been minimized.

@ijackson
Copy link
Author

ijackson commented Feb 2, 2022

@rustbot modify labels +regression-from-stable-to-nightly

@rustbot

This comment has been minimized.

@xFrednet
Copy link
Member

xFrednet commented Feb 2, 2022

What label isn't listed as an allowed label in our configuration file, I didn't even knew we had it until now ^^

@Jarcho
Copy link
Contributor

Jarcho commented Feb 3, 2022

Trait functions will no longer suggest &mut changes as of #8369.

@ijackson
Copy link
Author

ijackson commented Feb 3, 2022

Ah, cool, thanks. Closing this as a dupe of #8366

@ijackson ijackson closed this as completed Feb 3, 2022
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

4 participants