diff options
| author | Michael Goulet <michael@errs.io> | 2024-12-31 01:20:45 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2024-12-31 02:21:17 +0000 |
| commit | f28e13b0552ab7b95e0cc764c5285ebac23fcaa2 (patch) | |
| tree | 89e917842223e921196ebe9e43a859916c5f573c | |
| parent | 6a3474e653cbd77941f540852027f136c1d741c4 (diff) | |
| download | rust-f28e13b0552ab7b95e0cc764c5285ebac23fcaa2.tar.gz rust-f28e13b0552ab7b95e0cc764c5285ebac23fcaa2.zip | |
Fix span for IndexMut method call on HashMap/BTreeMap
| -rw-r--r-- | compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs | 4 | ||||
| -rw-r--r-- | tests/ui/borrowck/index-mut-help.stderr | 5 | ||||
| -rw-r--r-- | tests/ui/issues/issue-41726.stderr | 5 |
3 files changed, 10 insertions, 4 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 109ec096417..c690789b587 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -624,7 +624,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { self.suggested = true; } else if let hir::ExprKind::MethodCall(_path, receiver, _, sp) = expr.kind && let hir::ExprKind::Index(val, index, _) = receiver.kind - && expr.span == self.assign_span + && receiver.span == self.assign_span { // val[index].path(args..); self.err.multipart_suggestion( @@ -639,7 +639,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { index.span.shrink_to_hi().with_hi(receiver.span.hi()), ") { val".to_string(), ), - (sp.shrink_to_hi(), "); }".to_string()), + (sp.shrink_to_hi(), "; }".to_string()), ], Applicability::MachineApplicable, ); diff --git a/tests/ui/borrowck/index-mut-help.stderr b/tests/ui/borrowck/index-mut-help.stderr index 8766ff4a6c4..c4c9c1c5313 100644 --- a/tests/ui/borrowck/index-mut-help.stderr +++ b/tests/ui/borrowck/index-mut-help.stderr @@ -5,7 +5,10 @@ LL | map["peter"].clear(); | ^^^^^^^^^^^^ cannot borrow as mutable | = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<&str, String>` - = help: to modify a `HashMap<&str, String>`, use `.get_mut()`, `.insert()` or the entry API +help: to modify a `HashMap<&str, String>` use `.get_mut()` + | +LL | if let Some(val) = map.get_mut("peter") { val.clear(); }; + | ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~ +++ error[E0594]: cannot assign to data in an index of `HashMap<&str, String>` --> $DIR/index-mut-help.rs:11:5 diff --git a/tests/ui/issues/issue-41726.stderr b/tests/ui/issues/issue-41726.stderr index fe7d4df7067..250bba222bf 100644 --- a/tests/ui/issues/issue-41726.stderr +++ b/tests/ui/issues/issue-41726.stderr @@ -5,7 +5,10 @@ LL | things[src.as_str()].sort(); | ^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable | = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<String, Vec<String>>` - = help: to modify a `HashMap<String, Vec<String>>`, use `.get_mut()`, `.insert()` or the entry API +help: to modify a `HashMap<String, Vec<String>>` use `.get_mut()` + | +LL | if let Some(val) = things.get_mut(src.as_str()) { val.sort(); }; + | ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~ +++ error: aborting due to 1 previous error |
