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

Wrong structured suggestion for mutability change in for loop pattern #82082

Closed
estebank opened this issue Feb 14, 2021 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Given:

use std::collections::HashMap;

struct Test {
    v: u32,
}

fn main() {
    let mut map = HashMap::new();
    map.insert("a", Test { v: 0 });

    for (k, v) in map.iter() {
        v.v += 1;
    }
}

we emit:

error[E0594]: cannot assign to `v.v` which is behind a `&` reference
  --> src/main.rs:12:9
   |
11 |     for (k, v) in map.iter() {
   |             - help: consider changing this to be a mutable reference: `&mut Test`
12 |         v.v += 1;
   |         ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written

when what it should be suggesting is

error[E0594]: cannot assign to `v.v` which is behind a `&` reference
  --> src/main.rs:12:9
   |
11 |     for (k, v) in map.iter() {
   |             - help: consider changing making this binding mutable: `mut v`
12 |         v.v += 1;
   |         ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written

CC #82081 #49839.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Feb 14, 2021
@estebank
Copy link
Contributor Author

We now properly suggest calling .iter_mut() instead

error[E0594]: cannot assign to `v.v`, which is behind a `&` reference
  --> src/main.rs:12:9
   |
11 |     for (k, v) in map.iter() {
   |                   ----------
   |                   |   |
   |                   |   help: use mutable method: `iter_mut()`
   |                   this iterator yields `&` references
12 |         v.v += 1;
   |         ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written

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` D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant