Skip to content

Commit 6a3474e

Browse files
Use if-let in structured suggestion instead of Option::map
1 parent b994124 commit 6a3474e

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
575575
// ---------- place
576576
self.err.multipart_suggestions(
577577
format!(
578-
"use `.insert()` to insert a value into a `{}`, `.get_mut()` to modify it, or the entry API for more flexibility",
578+
"use `.insert()` to insert a value into a `{}`, `.get_mut()` \
579+
to modify it, or the entry API for more flexibility",
579580
self.ty,
580581
),
581582
vec![
@@ -592,16 +593,17 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
592593
(rv.span.shrink_to_hi(), ")".to_string()),
593594
],
594595
vec![
595-
// val.get_mut(index).map(|v| { *v = rv; });
596+
// if let Some(v) = val.get_mut(index) { *v = rv; }
597+
(val.span.shrink_to_lo(), "if let Some(val) = ".to_string()),
596598
(
597599
val.span.shrink_to_hi().with_hi(index.span.lo()),
598600
".get_mut(".to_string(),
599601
),
600602
(
601603
index.span.shrink_to_hi().with_hi(place.span.hi()),
602-
").map(|val| { *val".to_string(),
604+
") { *val".to_string(),
603605
),
604-
(rv.span.shrink_to_hi(), "; })".to_string()),
606+
(rv.span.shrink_to_hi(), "; }".to_string()),
605607
],
606608
vec![
607609
// let x = val.entry(index).or_insert(rv);
@@ -628,15 +630,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
628630
self.err.multipart_suggestion(
629631
format!("to modify a `{}` use `.get_mut()`", self.ty),
630632
vec![
633+
(val.span.shrink_to_lo(), "if let Some(val) = ".to_string()),
631634
(
632635
val.span.shrink_to_hi().with_hi(index.span.lo()),
633636
".get_mut(".to_string(),
634637
),
635638
(
636639
index.span.shrink_to_hi().with_hi(receiver.span.hi()),
637-
").map(|val| val".to_string(),
640+
") { val".to_string(),
638641
),
639-
(sp.shrink_to_hi(), ")".to_string()),
642+
(sp.shrink_to_hi(), "); }".to_string()),
640643
],
641644
Applicability::MachineApplicable,
642645
);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ help: use `.insert()` to insert a value into a `HashMap<&str, String>`, `.get_mu
1818
|
1919
LL | map.insert("peter", "0".to_string());
2020
| ~~~~~~~~ ~ +
21-
LL | map.get_mut("peter").map(|val| { *val = "0".to_string(); });
22-
| ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ++++
21+
LL | if let Some(val) = map.get_mut("peter") { *val = "0".to_string(); };
22+
| ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~~ +++
2323
LL | let val = map.entry("peter").or_insert("0".to_string());
2424
| +++++++++ ~~~~~~~ ~~~~~~~~~~~~ +
2525

tests/ui/btreemap/btreemap-index-mut-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ help: use `.insert()` to insert a value into a `BTreeMap<u32, u32>`, `.get_mut()
99
|
1010
LL | map.insert(&0, 1);
1111
| ~~~~~~~~ ~ +
12-
LL | map.get_mut(&0).map(|val| { *val = 1; });
13-
| ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ++++
12+
LL | if let Some(val) = map.get_mut(&0) { *val = 1; };
13+
| ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~~ +++
1414
LL | let val = map.entry(&0).or_insert(1);
1515
| +++++++++ ~~~~~~~ ~~~~~~~~~~~~ +
1616

tests/ui/btreemap/btreemap-index-mut.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ help: use `.insert()` to insert a value into a `BTreeMap<u32, u32>`, `.get_mut()
99
|
1010
LL | map.insert(&0, 1);
1111
| ~~~~~~~~ ~ +
12-
LL | map.get_mut(&0).map(|val| { *val = 1; });
13-
| ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ++++
12+
LL | if let Some(val) = map.get_mut(&0) { *val = 1; };
13+
| ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~~ +++
1414
LL | let val = map.entry(&0).or_insert(1);
1515
| +++++++++ ~~~~~~~ ~~~~~~~~~~~~ +
1616

tests/ui/hashmap/hashmap-index-mut.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ help: use `.insert()` to insert a value into a `HashMap<u32, u32>`, `.get_mut()`
99
|
1010
LL | map.insert(&0, 1);
1111
| ~~~~~~~~ ~ +
12-
LL | map.get_mut(&0).map(|val| { *val = 1; });
13-
| ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ++++
12+
LL | if let Some(val) = map.get_mut(&0) { *val = 1; };
13+
| ++++++++++++++++++ ~~~~~~~~~ ~~~~~~~~ +++
1414
LL | let val = map.entry(&0).or_insert(1);
1515
| +++++++++ ~~~~~~~ ~~~~~~~~~~~~ +
1616

0 commit comments

Comments
 (0)