Skip to content

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

Closed
@estebank

Description

@estebank

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions