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

Suggest borrowing when "value moved here" points at for loop head #61108

Closed
estebank opened this issue May 24, 2019 · 0 comments · Fixed by #61144
Closed

Suggest borrowing when "value moved here" points at for loop head #61108

estebank opened this issue May 24, 2019 · 0 comments · Fixed by #61144
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@estebank
Copy link
Contributor

estebank commented May 24, 2019

Given

fn main() {
    let mut bad_letters = vec!['e', 't', 'o', 'i'];
    for l in bad_letters {
        // do something here
    }
    bad_letters.push('s');
}

we should suggest borrowing bad_letters:

error[E0382]: borrow of moved value: `bad_letters`
 --> src/main.rs:6:5
  |
2 |     let mut bad_letters = vec!['e', 't', 'o', 'i'];
  |         --------------- move occurs because `bad_letters` has type `std::vec::Vec<char>`, which does not implement the `Copy` trait
3 |     for l in bad_letters {
  |              -----------
  |              |
  |              value moved here
  |              help: consider borrowing here: `&bad_letters`
...
6 |     bad_letters.push('s');
  |     ^^^^^^^^^^^ value borrowed here after move
  |
  = note: probably some explanation about `Iterator::into_iter`

This could potentially be incorrect without further analysis, but it should be correct in the majority of the cases that newcomers would face.

From https://thenewwazoo.github.io/clone.html

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels May 24, 2019
Lonami referenced this issue in thenewwazoo/thenewwazoo.github.io May 24, 2019
Centril added a commit to Centril/rust that referenced this issue May 25, 2019
Centril added a commit to Centril/rust that referenced this issue May 26, 2019
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant