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

diagnostics: regression test for HashMap iter_mut suggestion #95415

Merged
merged 2 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
_,
[
Expr {
kind: MethodCall(path_segment, ..),
kind:
MethodCall(
path_segment,
_args,
span,
),
hir_id,
..
},
Expand Down Expand Up @@ -831,7 +836,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if let Some(mut suggestions) = opt_suggestions {
if suggestions.peek().is_some() {
err.span_suggestions(
path_segment.ident.span,
*span,
"use mutable method",
suggestions,
Applicability::MaybeIncorrect,
Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// run-rustfix
// https://github.com/rust-lang/rust/issues/82081

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_mut() {
//~^ HELP use mutable method
//~| NOTE this iterator yields `&` references
v.v += 1;
//~^ ERROR cannot assign to `v.v`
//~| NOTE `v` is a `&` reference
}
}
21 changes: 21 additions & 0 deletions src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// run-rustfix
// https://github.com/rust-lang/rust/issues/82081

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() {
//~^ HELP use mutable method
//~| NOTE this iterator yields `&` references
v.v += 1;
//~^ ERROR cannot assign to `v.v`
//~| NOTE `v` is a `&` reference
}
}
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0594]: cannot assign to `v.v`, which is behind a `&` reference
--> $DIR/suggest-mut-method-for-loop-hashmap.rs:17:9
|
LL | for (_k, mut v) in map.iter() {
| ----------
| | |
| | help: use mutable method: `iter_mut()`
| this iterator yields `&` references
...
LL | v.v += 1;
| ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written

error: aborting due to previous error

For more information about this error, try `rustc --explain E0594`.