Skip to content

Commit 757ab6b

Browse files
committed
diagnostics: add regression test for #82081
1 parent a063b3a commit 757ab6b

3 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
// https://github.com/rust-lang/rust/issues/82081
3+
4+
use std::collections::HashMap;
5+
6+
struct Test {
7+
v: u32,
8+
}
9+
10+
fn main() {
11+
let mut map = HashMap::new();
12+
map.insert("a", Test { v: 0 });
13+
14+
for (_k, mut v) in map.iter_mut() {
15+
//~^ HELP use mutable method
16+
//~| NOTE this iterator yields `&` references
17+
v.v += 1;
18+
//~^ ERROR cannot assign to `v.v`
19+
//~| NOTE `v` is a `&` reference
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
// https://github.com/rust-lang/rust/issues/82081
3+
4+
use std::collections::HashMap;
5+
6+
struct Test {
7+
v: u32,
8+
}
9+
10+
fn main() {
11+
let mut map = HashMap::new();
12+
map.insert("a", Test { v: 0 });
13+
14+
for (_k, mut v) in map.iter() {
15+
//~^ HELP use mutable method
16+
//~| NOTE this iterator yields `&` references
17+
v.v += 1;
18+
//~^ ERROR cannot assign to `v.v`
19+
//~| NOTE `v` is a `&` reference
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0594]: cannot assign to `v.v`, which is behind a `&` reference
2+
--> $DIR/suggest-mut-method-for-loop-hashmap.rs:17:9
3+
|
4+
LL | for (_k, mut v) in map.iter() {
5+
| ----------
6+
| | |
7+
| | help: use mutable method: `iter_mut()`
8+
| this iterator yields `&` references
9+
...
10+
LL | v.v += 1;
11+
| ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0594`.

0 commit comments

Comments
 (0)