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

fix unnecessary_to_owned about msrv #8692

Merged
merged 1 commit into from
Apr 13, 2022

Conversation

kyoto7250
Copy link
Contributor

@kyoto7250 kyoto7250 commented Apr 12, 2022

This PR fixes [`unnecessary_owned`].

What

# 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.

Why

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

@rust-highfive
Copy link

r? @giraffate

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 12, 2022
@giraffate
Copy link
Contributor

@bors r+

It looks good, thanks!

@bors
Copy link
Contributor

bors commented Apr 13, 2022

📌 Commit dfdc5ad has been approved by giraffate

@bors
Copy link
Contributor

bors commented Apr 13, 2022

⌛ Testing commit dfdc5ad with merge f70c73f...

@bors
Copy link
Contributor

bors commented Apr 13, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: giraffate
Pushing f70c73f to master...

@bors bors merged commit f70c73f into rust-lang:master Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants