about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-03-29 17:11:52 +0200
committerGitHub <noreply@github.com>2022-03-29 17:11:52 +0200
commiteceb173de9ee0cad205a139249a4d043fe251e0e (patch)
treeab7e1a8ba4ce55e655ee959959059a42248ba82b
parent1830e8fdcd047c986e4d7fcdf3a1f8ae1a53deeb (diff)
parent757ab6b55c1e06689d457550afe5da3955a22c3a (diff)
downloadrust-eceb173de9ee0cad205a139249a4d043fe251e0e.tar.gz
rust-eceb173de9ee0cad205a139249a4d043fe251e0e.zip
Rollup merge of #95415 - notriddle:notriddle/issue-82081, r=Dylan-DPC
diagnostics: regression test for HashMap iter_mut suggestion

Closes #82081
-rw-r--r--compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs9
-rw-r--r--src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.fixed21
-rw-r--r--src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.rs21
-rw-r--r--src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.stderr15
4 files changed, 64 insertions, 2 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
index c77bbeb86e5..6e551e95cbb 100644
--- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
@@ -787,7 +787,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
                                                         _,
                                                         [
                                                             Expr {
-                                                                kind: MethodCall(path_segment, ..),
+                                                                kind:
+                                                                    MethodCall(
+                                                                        path_segment,
+                                                                        _args,
+                                                                        span,
+                                                                    ),
                                                                 hir_id,
                                                                 ..
                                                             },
@@ -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,
diff --git a/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.fixed b/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.fixed
new file mode 100644
index 00000000000..b69bad98888
--- /dev/null
+++ b/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.fixed
@@ -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
+    }
+}
diff --git a/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.rs b/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.rs
new file mode 100644
index 00000000000..9284410dfa3
--- /dev/null
+++ b/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.rs
@@ -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
+    }
+}
diff --git a/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.stderr b/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.stderr
new file mode 100644
index 00000000000..74433daa6ac
--- /dev/null
+++ b/src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.stderr
@@ -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`.