Skip to content

Commit eceb173

Browse files
authored
Rollup merge of #95415 - notriddle:notriddle/issue-82081, r=Dylan-DPC
diagnostics: regression test for HashMap iter_mut suggestion Closes #82081
2 parents 1830e8f + 757ab6b commit eceb173

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
787787
_,
788788
[
789789
Expr {
790-
kind: MethodCall(path_segment, ..),
790+
kind:
791+
MethodCall(
792+
path_segment,
793+
_args,
794+
span,
795+
),
791796
hir_id,
792797
..
793798
},
@@ -831,7 +836,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
831836
if let Some(mut suggestions) = opt_suggestions {
832837
if suggestions.peek().is_some() {
833838
err.span_suggestions(
834-
path_segment.ident.span,
839+
*span,
835840
"use mutable method",
836841
suggestions,
837842
Applicability::MaybeIncorrect,
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)