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

Don't suggest extra clone when converting cloned slice to Vec #53692

Closed
sunjay opened this issue Aug 25, 2018 · 1 comment
Closed

Don't suggest extra clone when converting cloned slice to Vec #53692

sunjay opened this issue Aug 25, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@sunjay
Copy link
Member

sunjay commented Aug 25, 2018

Let's say you have the following (simplified) code: (Rust Playground)

fn main() {
    let items = vec![1, 2, 3];
    let ref_items: &[i32] = &items;
    let items_clone: Vec<i32> = ref_items.clone();
}

This results in the following error:

error[E0308]: mismatched types
 --> src/main.rs:4:33
  |
4 |     let items_clone: Vec<i32> = ref_items.clone();
  |                                 ^^^^^^^^^^^^^^^^^
  |                                 |
  |                                 expected struct `std::vec::Vec`, found &[i32]
  |                                 help: try using a conversion method: `ref_items.clone().to_vec()`
  |
  = note: expected type `std::vec::Vec<i32>`
             found type `&[i32]`

The suggested fix ref_items.clone().to_vec() will work, but to_vec already copies the data so the extra clone is wasteful. The error message should instead suggest ref_items.to_vec().

This is an error you can run into if ref_items was previously of type &Vec<i32> and you changed it to a slice after writing the rest of the code.

@Havvy Havvy added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 25, 2018
@csmoe
Copy link
Member

csmoe commented Aug 25, 2018

Note: this kind of improvement applies to &str too:
https://play.rust-lang.org/?gist=187e7bb009a976693176dcb45d3fdc8a&version=stable&mode=debug&edition=2015

fn main() {
    let s = "hi";
    let string: String = s.clone();
}

Err:

error[E0308]: mismatched types
 --> src/main.rs:3:26
  |
3 |     let string: String = s.clone();
  |                          ^^^^^^^^^
  |                          |
  |                          expected struct `std::string::String`, found &str
  |                          help: try using a conversion method: `s.clone().to_string()`
  |
  = note: expected type `std::string::String`
             found type `&str`

PramodBisht added a commit to PramodBisht/rust that referenced this issue Sep 9, 2018
bors added a commit that referenced this issue Sep 14, 2018
Addressed #53692

@sunjay @estebank  @csmoe hopefully this answer #53692
Please let me know if you have any suggestion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

4 participants