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 false positive on ToOwned::clone_into() #10612

Closed
kpreid opened this issue Apr 9, 2023 · 0 comments · Fixed by #13313
Closed

ptr_arg false positive on ToOwned::clone_into() #10612

kpreid opened this issue Apr 9, 2023 · 0 comments · Fixed by #13313
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@kpreid
Copy link
Contributor

kpreid commented Apr 9, 2023

Summary

ptr_arg does not recognize ToOwned::clone_into() as a call that requires &mut String or &mut Vec<T>, and incorrectly suggests changing to &mut str or &mut [T].

Lint Name

ptr_arg

Reproducer

I tried this code:

pub fn t1(mut_string: &mut String, ref_str: &str) {
    ref_str.clone_into(mut_string);
}
pub fn t2(mut_vec: &mut Vec<u8>, ref_slice: &[u8]) {
    ref_slice.clone_into(mut_vec);
}

I saw this happen:

warning: writing `&mut String` instead of `&mut str` involves a new object where a slice will do
 --> src/lib.rs:1:23
  |
1 | pub fn t1(mut_string: &mut String, ref_str: &str) {
  |                       ^^^^^^^^^^^ help: change this to: `&mut str`
  |
  = 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

warning: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
 --> src/lib.rs:4:20
  |
4 | pub fn t2(mut_vec: &mut Vec<u8>, ref_slice: &[u8]) {
  |                    ^^^^^^^^^^^^ help: change this to: `&mut [u8]`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg

The suggestions do not compile.

I expected to see this happen: No warnings, since clone_into requires a mutable reference to the owned and resizable type.

Version

Happens with 0.1.70 (2023-04-02 3a8a131) and also latest git 9818ad21678e5e71dcdfd3e3be5a348195e172e0

Additional Labels

@rustbot label +I-suggestion-causes-error

@kpreid kpreid 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 Apr 9, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 9, 2023
@bors bors closed this as completed in 2536745 Sep 13, 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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants