Skip to content

Bad suggestion when calling iter instead of iter_mut #82081

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
estebank opened this issue Feb 14, 2021 · 0 comments · Fixed by #95415
Closed

Bad suggestion when calling iter instead of iter_mut #82081

estebank opened this issue Feb 14, 2021 · 0 comments · Fixed by #95415
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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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, mut 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, mut 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

but we should be instead suggesting

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

CC #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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Feb 14, 2021
notriddle added a commit to notriddle/rust that referenced this issue Mar 28, 2022
@bors bors closed this as completed in eceb173 Mar 29, 2022
# for free to join this conversation on GitHub. Already have an account? # 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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

Successfully merging a pull request may close this issue.

1 participant