From a063b3a4b6eed93d07c084d6e34dbe7d02e02b04 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 28 Mar 2022 11:47:01 -0700 Subject: [PATCH 1/2] diagnostics: do not suggest `map.iter_mut()()` --- .../rustc_borrowck/src/diagnostics/mutability_errors.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index c77bbeb86e586..6e551e95cbb43 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, From 757ab6b55c1e06689d457550afe5da3955a22c3a Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 28 Mar 2022 11:47:19 -0700 Subject: [PATCH 2/2] diagnostics: add regression test for #82081 --- .../suggest-mut-method-for-loop-hashmap.fixed | 21 +++++++++++++++++++ .../suggest-mut-method-for-loop-hashmap.rs | 21 +++++++++++++++++++ ...suggest-mut-method-for-loop-hashmap.stderr | 15 +++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.fixed create mode 100644 src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.rs create mode 100644 src/test/ui/suggestions/suggest-mut-method-for-loop-hashmap.stderr 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 0000000000000..b69bad98888e3 --- /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 0000000000000..9284410dfa383 --- /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 0000000000000..74433daa6acf2 --- /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`.