mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Rollup merge of #95415 - notriddle:notriddle/issue-82081, r=Dylan-DPC
diagnostics: regression test for HashMap iter_mut suggestion Closes #82081
This commit is contained in:
commit
eceb173de9
@ -787,7 +787,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||||||
_,
|
_,
|
||||||
[
|
[
|
||||||
Expr {
|
Expr {
|
||||||
kind: MethodCall(path_segment, ..),
|
kind:
|
||||||
|
MethodCall(
|
||||||
|
path_segment,
|
||||||
|
_args,
|
||||||
|
span,
|
||||||
|
),
|
||||||
hir_id,
|
hir_id,
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
@ -831,7 +836,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||||||
if let Some(mut suggestions) = opt_suggestions {
|
if let Some(mut suggestions) = opt_suggestions {
|
||||||
if suggestions.peek().is_some() {
|
if suggestions.peek().is_some() {
|
||||||
err.span_suggestions(
|
err.span_suggestions(
|
||||||
path_segment.ident.span,
|
*span,
|
||||||
"use mutable method",
|
"use mutable method",
|
||||||
suggestions,
|
suggestions,
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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`.
|
Loading…
Reference in New Issue
Block a user