Skip to content

Commit f28e13b

Browse files
Fix span for IndexMut method call on HashMap/BTreeMap
1 parent 6a3474e commit f28e13b

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
624624
self.suggested = true;
625625
} else if let hir::ExprKind::MethodCall(_path, receiver, _, sp) = expr.kind
626626
&& let hir::ExprKind::Index(val, index, _) = receiver.kind
627-
&& expr.span == self.assign_span
627+
&& receiver.span == self.assign_span
628628
{
629629
// val[index].path(args..);
630630
self.err.multipart_suggestion(
@@ -639,7 +639,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
639639
index.span.shrink_to_hi().with_hi(receiver.span.hi()),
640640
") { val".to_string(),
641641
),
642-
(sp.shrink_to_hi(), "); }".to_string()),
642+
(sp.shrink_to_hi(), "; }".to_string()),
643643
],
644644
Applicability::MachineApplicable,
645645
);

tests/ui/borrowck/index-mut-help.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ LL | map["peter"].clear();
55
| ^^^^^^^^^^^^ cannot borrow as mutable
66
|
77
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<&str, String>`
8-
= help: to modify a `HashMap<&str, String>`, use `.get_mut()`, `.insert()` or the entry API
8+
help: to modify a `HashMap<&str, String>` use `.get_mut()`
9+
|
10+
LL | if let Some(val) = map.get_mut("peter") { val.clear(); };
11+
| ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~ +++
912

1013
error[E0594]: cannot assign to data in an index of `HashMap<&str, String>`
1114
--> $DIR/index-mut-help.rs:11:5

tests/ui/issues/issue-41726.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ LL | things[src.as_str()].sort();
55
| ^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
66
|
77
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<String, Vec<String>>`
8-
= help: to modify a `HashMap<String, Vec<String>>`, use `.get_mut()`, `.insert()` or the entry API
8+
help: to modify a `HashMap<String, Vec<String>>` use `.get_mut()`
9+
|
10+
LL | if let Some(val) = things.get_mut(src.as_str()) { val.sort(); };
11+
| ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~ +++
912

1013
error: aborting due to 1 previous error
1114

0 commit comments

Comments
 (0)