4

fix unnecessary_to_owned about msrv by kyoto7250 · Pull Request #8692 · rust-lan...

 2 years ago
source link: https://github.com/rust-lang/rust-clippy/pull/8692
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Copy link

Contributor

kyoto7250 commented 10 days ago

edited by giraffate

This PR fixes [`unnecessary_owned`].

# sample code
fn _msrv_1_35() {
    #![clippy::msrv = "1.35"]
    let _ = &["x"][..].to_vec().into_iter();
}

fn _msrv_1_36() {
    #![clippy::msrv = "1.36"]
    let _ = &["x"][..].to_vec().into_iter();
}

If we will check this code using clippy, [`unnecessary_owned`] will modify the code as follows.

error: unnecessary use of `to_vec`
  --> $DIR/unnecessary_to_owned.rs:219:14
   |
LL |     let _ = &["x"][..].to_vec().into_iter();
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().copied()`

error: unnecessary use of `to_vec`
  --> $DIR/unnecessary_to_owned.rs:224:14
   |
LL |     let _ = &["x"][..].to_vec().into_iter();
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().copied()`

This is incorrect. Because Iterator::copied was estabilished in 1.36.

This bug was caused by not separating "copied" and "clone" by reference to msrv.

let cloned_or_copied = if is_copy(cx, item_ty) { "copied" } else { "cloned" };

So, I added a conditional branch and described the corresponding test.

Thank you in advance.

changelog: fix wrong suggestions about msrv in [unnecessary_to_owned]

r! @giraffate


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK