Skip to content

Commit 4ff83ce

Browse files
author
mejrs
committed
Deduplicate some logic
1 parent e9224b3 commit 4ff83ce

File tree

4 files changed

+42
-62
lines changed

4 files changed

+42
-62
lines changed

compiler/rustc_hir_analysis/src/check/method/suggest.rs

+27-47
Original file line numberDiff line numberDiff line change
@@ -1521,67 +1521,47 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15211521
use one of the `assume_init` methods to access the inner value"
15221522
));
15231523
} else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
1524-
match mutable {
1525-
Some(Mutability::Not) => {
1526-
err.span_suggestion_verbose(
1527-
expr.span.shrink_to_hi(),
1528-
format!(
1529-
"use `.borrow()` to borrow the `{ty}`, \
1530-
panicking if any outstanding mutable borrows exist."
1531-
),
1532-
".borrow()",
1533-
Applicability::MaybeIncorrect,
1534-
);
1535-
}
1524+
let (suggestion, borrow_kind, panic_if) = match mutable {
1525+
Some(Mutability::Not) => (".borrow()", "borrow", "a mutable borrow exists"),
15361526
Some(Mutability::Mut) => {
1537-
err.span_suggestion_verbose(
1538-
expr.span.shrink_to_hi(),
1539-
format!(
1540-
"use `.borrow_mut()` to mutably borrow the `{ty}`, \
1541-
panicking if any outstanding borrows exist."
1542-
),
1543-
".borrow_mut()",
1544-
Applicability::MaybeIncorrect,
1545-
);
1527+
(".borrow_mut()", "mutably borrow", "any borrows exist")
15461528
}
15471529
None => return,
1548-
}
1530+
};
1531+
err.span_suggestion_verbose(
1532+
expr.span.shrink_to_hi(),
1533+
format!(
1534+
"use `{suggestion}` to {borrow_kind} the `{ty}`, \
1535+
panicking if {panic_if}"
1536+
),
1537+
suggestion,
1538+
Applicability::MaybeIncorrect,
1539+
);
15491540
} else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
15501541
err.span_suggestion_verbose(
15511542
expr.span.shrink_to_hi(),
15521543
format!(
1553-
"use `.lock()` to borrow the `{ty}`, \
1544+
"use `.lock().unwrap()` to borrow the `{ty}`, \
15541545
blocking the current thread until it can be acquired"
15551546
),
15561547
".lock().unwrap()",
15571548
Applicability::MaybeIncorrect,
15581549
);
15591550
} else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
1560-
match mutable {
1561-
Some(Mutability::Not) => {
1562-
err.span_suggestion_verbose(
1563-
expr.span.shrink_to_hi(),
1564-
format!(
1565-
"use `.read()` to borrow the `{ty}`, \
1566-
blocking the current thread until it can be acquired"
1567-
),
1568-
".read().unwrap()",
1569-
Applicability::MaybeIncorrect,
1570-
);
1571-
}
1572-
Some(Mutability::Mut) => {
1573-
err.span_suggestion_verbose(
1574-
expr.span.shrink_to_hi(),
1575-
format!(
1576-
"use `.write()` to mutably borrow the `{ty}`, \
1577-
blocking the current thread until it can be acquired"
1578-
),
1579-
".write().unwrap()",
1580-
Applicability::MaybeIncorrect,
1581-
);
1582-
}
1551+
let (suggestion, borrow_kind) = match mutable {
1552+
Some(Mutability::Not) => (".read().unwrap()", "borrow"),
1553+
Some(Mutability::Mut) => (".write().unwrap()", "mutably borrow"),
15831554
None => return,
1584-
}
1555+
};
1556+
err.span_suggestion_verbose(
1557+
expr.span.shrink_to_hi(),
1558+
format!(
1559+
"use `{suggestion}` to {borrow_kind} the `{ty}`, \
1560+
blocking the current thread until it can be acquired"
1561+
),
1562+
suggestion,
1563+
Applicability::MaybeIncorrect,
1564+
);
15851565
} else {
15861566
return;
15871567
};

src/test/ui/suggestions/inner_type.fixed

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@ fn main() {
1616

1717
other_item.borrow().method();
1818
//~^ ERROR no method named `method` found for struct `RefCell` in the current scope [E0599]
19-
//~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist.
19+
//~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if a mutable borrow exists
2020

2121
other_item.borrow_mut().some_mutable_method();
2222
//~^ ERROR no method named `some_mutable_method` found for struct `RefCell` in the current scope [E0599]
23-
//~| HELP use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist.
23+
//~| HELP .borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any borrows exist
2424

2525
let another_item = std::sync::Mutex::new(Struct { p: 42_u32 });
2626

2727
another_item.lock().unwrap().method();
2828
//~^ ERROR no method named `method` found for struct `Mutex` in the current scope [E0599]
29-
//~| HELP use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
29+
//~| HELP use `.lock().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
3030

3131
let another_item = std::sync::RwLock::new(Struct { p: 42_u32 });
3232

3333
another_item.read().unwrap().method();
3434
//~^ ERROR no method named `method` found for struct `RwLock` in the current scope [E0599]
35-
//~| HELP use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
35+
//~| HELP use `.read().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
3636

3737
another_item.write().unwrap().some_mutable_method();
3838
//~^ ERROR no method named `some_mutable_method` found for struct `RwLock` in the current scope [E0599]
39-
//~| HELP use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
39+
//~| HELP use `.write().unwrap()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
4040
}

src/test/ui/suggestions/inner_type.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@ fn main() {
1616

1717
other_item.method();
1818
//~^ ERROR no method named `method` found for struct `RefCell` in the current scope [E0599]
19-
//~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist.
19+
//~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if a mutable borrow exists
2020

2121
other_item.some_mutable_method();
2222
//~^ ERROR no method named `some_mutable_method` found for struct `RefCell` in the current scope [E0599]
23-
//~| HELP use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist.
23+
//~| HELP .borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any borrows exist
2424

2525
let another_item = std::sync::Mutex::new(Struct { p: 42_u32 });
2626

2727
another_item.method();
2828
//~^ ERROR no method named `method` found for struct `Mutex` in the current scope [E0599]
29-
//~| HELP use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
29+
//~| HELP use `.lock().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
3030

3131
let another_item = std::sync::RwLock::new(Struct { p: 42_u32 });
3232

3333
another_item.method();
3434
//~^ ERROR no method named `method` found for struct `RwLock` in the current scope [E0599]
35-
//~| HELP use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
35+
//~| HELP use `.read().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
3636

3737
another_item.some_mutable_method();
3838
//~^ ERROR no method named `some_mutable_method` found for struct `RwLock` in the current scope [E0599]
39-
//~| HELP use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
39+
//~| HELP use `.write().unwrap()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
4040
}

src/test/ui/suggestions/inner_type.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: the method `method` exists on the type `Struct<u32>`
99
|
1010
LL | pub fn method(&self) {}
1111
| ^^^^^^^^^^^^^^^^^^^^
12-
help: use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist.
12+
help: use `.borrow()` to borrow the `Struct<u32>`, panicking if a mutable borrow exists
1313
|
1414
LL | other_item.borrow().method();
1515
| +++++++++
@@ -25,7 +25,7 @@ note: the method `some_mutable_method` exists on the type `Struct<u32>`
2525
|
2626
LL | pub fn some_mutable_method(&mut self) {}
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28-
help: use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist.
28+
help: use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any borrows exist
2929
|
3030
LL | other_item.borrow_mut().some_mutable_method();
3131
| +++++++++++++
@@ -41,7 +41,7 @@ note: the method `method` exists on the type `Struct<u32>`
4141
|
4242
LL | pub fn method(&self) {}
4343
| ^^^^^^^^^^^^^^^^^^^^
44-
help: use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
44+
help: use `.lock().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
4545
|
4646
LL | another_item.lock().unwrap().method();
4747
| ++++++++++++++++
@@ -57,7 +57,7 @@ note: the method `method` exists on the type `Struct<u32>`
5757
|
5858
LL | pub fn method(&self) {}
5959
| ^^^^^^^^^^^^^^^^^^^^
60-
help: use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
60+
help: use `.read().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
6161
|
6262
LL | another_item.read().unwrap().method();
6363
| ++++++++++++++++
@@ -73,7 +73,7 @@ note: the method `some_mutable_method` exists on the type `Struct<u32>`
7373
|
7474
LL | pub fn some_mutable_method(&mut self) {}
7575
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76-
help: use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
76+
help: use `.write().unwrap()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
7777
|
7878
LL | another_item.write().unwrap().some_mutable_method();
7979
| +++++++++++++++++

0 commit comments

Comments
 (0)