Skip to content

Commit c43a91f

Browse files
committed
Suggest use .get_mut instead of &mut when overloaded index type not impl IndexMut
Signed-off-by: xizheyin <[email protected]>
1 parent aa50ab3 commit c43a91f

File tree

3 files changed

+79
-11
lines changed

3 files changed

+79
-11
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ pub(super) enum BorrowedContentSource<'tcx> {
889889
DerefMutableRef,
890890
DerefSharedRef,
891891
OverloadedDeref(Ty<'tcx>),
892-
OverloadedIndex(Ty<'tcx>),
892+
OverloadedIndex(Ty<'tcx>, Ty<'tcx>),
893893
}
894894

895895
impl<'tcx> BorrowedContentSource<'tcx> {
@@ -905,7 +905,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
905905
_ => None,
906906
})
907907
.unwrap_or_else(|| format!("dereference of `{ty}`")),
908-
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{ty}`"),
908+
BorrowedContentSource::OverloadedIndex(ty, _) => format!("index of `{ty}`"),
909909
}
910910
}
911911

@@ -917,7 +917,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
917917
// Overloaded deref and index operators should be evaluated into a
918918
// temporary. So we don't need a description here.
919919
BorrowedContentSource::OverloadedDeref(_)
920-
| BorrowedContentSource::OverloadedIndex(_) => None,
920+
| BorrowedContentSource::OverloadedIndex(_, _) => None,
921921
}
922922
}
923923

@@ -935,7 +935,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
935935
_ => None,
936936
})
937937
.unwrap_or_else(|| format!("dereference of `{ty}`")),
938-
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{ty}`"),
938+
BorrowedContentSource::OverloadedIndex(ty, _) => format!("an index of `{ty}`"),
939939
}
940940
}
941941

@@ -951,7 +951,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
951951
} else if tcx.is_lang_item(trait_id, LangItem::Index)
952952
|| tcx.is_lang_item(trait_id, LangItem::IndexMut)
953953
{
954-
Some(BorrowedContentSource::OverloadedIndex(args.type_at(0)))
954+
Some(BorrowedContentSource::OverloadedIndex(args.type_at(0), args.type_at(1)))
955955
} else {
956956
None
957957
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
519519
but it is not implemented for `{ty}`",
520520
));
521521
}
522-
Some(BorrowedContentSource::OverloadedIndex(ty)) => {
522+
Some(BorrowedContentSource::OverloadedIndex(ty, _)) => {
523523
err.help(format!(
524524
"trait `IndexMut` is required to modify indexed content, \
525525
but it is not implemented for `{ty}`",
@@ -1196,7 +1196,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11961196
None
11971197
}
11981198
None => {
1199-
if name != kw::SelfLower {
1199+
let suggest_get_mut = self.suggest_get_mut_when_not_impl_index_mut(
1200+
local,
1201+
opt_assignment_rhs_span,
1202+
);
1203+
if suggest_get_mut.is_some() {
1204+
suggest_get_mut
1205+
} else if name != kw::SelfLower {
12001206
suggest_ampmut(
12011207
self.infcx.tcx,
12021208
local_decl.ty,
@@ -1414,6 +1420,66 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14141420
None => {}
14151421
}
14161422
}
1423+
1424+
/// check if the RHS is an overloaded index expression from hashmap or btreemap,
1425+
/// if so, suggest using .get_mut() instead of &mut, see issue #143732
1426+
/// For example
1427+
/// ```text
1428+
/// let mut map = HashMap::new();
1429+
/// let value = &map["key"];
1430+
/// ```
1431+
fn suggest_get_mut_when_not_impl_index_mut(
1432+
&self,
1433+
local: Local,
1434+
opt_assignment_rhs_span: Option<Span>,
1435+
) -> Option<AmpMutSugg> {
1436+
self.find_assignments(local)
1437+
.first()
1438+
.map(|&location| {
1439+
if let Some(mir::Statement {
1440+
source_info: _,
1441+
kind: mir::StatementKind::Assign(box (_, mir::Rvalue::Ref(_, _, place))),
1442+
..
1443+
}) = self.body[location.block].statements.get(location.statement_index)
1444+
&& let BorrowedContentSource::OverloadedIndex(ty, index_ty) =
1445+
self.borrowed_content_source(place.as_ref())
1446+
&& let Some(index_mut_trait) = self.infcx.tcx.lang_items().index_mut_trait()
1447+
&& !self
1448+
.infcx
1449+
.type_implements_trait(
1450+
index_mut_trait,
1451+
[ty, index_ty],
1452+
self.infcx.param_env,
1453+
)
1454+
.must_apply_modulo_regions()
1455+
{
1456+
if let Some(rhs_span) = opt_assignment_rhs_span
1457+
&& let Ok(rhs_str) =
1458+
self.infcx.tcx.sess.source_map().span_to_snippet(rhs_span)
1459+
&& let Some(content) = rhs_str.strip_prefix('&')
1460+
&& content.contains('[')
1461+
&& content.contains(']')
1462+
{
1463+
let bracket_start = content.find('[')?;
1464+
let bracket_end = content.rfind(']')?;
1465+
1466+
if bracket_start < bracket_end {
1467+
let map_part = &content[..bracket_start];
1468+
let key_part = &content[bracket_start + 1..bracket_end];
1469+
1470+
return Some(AmpMutSugg {
1471+
has_sugg: true,
1472+
span: rhs_span,
1473+
suggestion: format!("{}.get_mut({}).unwrap()", map_part, key_part),
1474+
additional: None,
1475+
});
1476+
}
1477+
}
1478+
}
1479+
None
1480+
})
1481+
.flatten()
1482+
}
14171483
}
14181484

14191485
struct BindingFinder {

tests/ui/borrowck/suggest-use-as-mut-for-map.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ LL | string.push_str("test");
66
|
77
help: consider changing this to be a mutable reference
88
|
9-
LL | let string = &mut map[&0];
10-
| +++
9+
LL - let string = &map[&0];
10+
LL + let string = map.get_mut(&0).unwrap();
11+
|
1112

1213
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
1314
--> $DIR/suggest-use-as-mut-for-map.rs:14:5
@@ -17,8 +18,9 @@ LL | string.push_str("test");
1718
|
1819
help: consider changing this to be a mutable reference
1920
|
20-
LL | let string = &mut map[&0];
21-
| +++
21+
LL - let string = &map[&0];
22+
LL + let string = map.get_mut(&0).unwrap();
23+
|
2224

2325
error[E0596]: cannot borrow `*string` as mutable, as it is behind a `&` reference
2426
--> $DIR/suggest-use-as-mut-for-map.rs:18:5

0 commit comments

Comments
 (0)