1
Fork 0

Deduplicate some logic

This commit is contained in:
mejrs 2022-09-28 02:36:58 +02:00
parent e9224b3796
commit 4ff83cee95
4 changed files with 42 additions and 62 deletions

View file

@ -1521,67 +1521,47 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
use one of the `assume_init` methods to access the inner value" use one of the `assume_init` methods to access the inner value"
)); ));
} else if tcx.is_diagnostic_item(sym::RefCell, inner_id) { } else if tcx.is_diagnostic_item(sym::RefCell, inner_id) {
match mutable { let (suggestion, borrow_kind, panic_if) = match mutable {
Some(Mutability::Not) => { Some(Mutability::Not) => (".borrow()", "borrow", "a mutable borrow exists"),
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `.borrow()` to borrow the `{ty}`, \
panicking if any outstanding mutable borrows exist."
),
".borrow()",
Applicability::MaybeIncorrect,
);
}
Some(Mutability::Mut) => { Some(Mutability::Mut) => {
err.span_suggestion_verbose( (".borrow_mut()", "mutably borrow", "any borrows exist")
expr.span.shrink_to_hi(),
format!(
"use `.borrow_mut()` to mutably borrow the `{ty}`, \
panicking if any outstanding borrows exist."
),
".borrow_mut()",
Applicability::MaybeIncorrect,
);
} }
None => return, None => return,
} };
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `{suggestion}` to {borrow_kind} the `{ty}`, \
panicking if {panic_if}"
),
suggestion,
Applicability::MaybeIncorrect,
);
} else if tcx.is_diagnostic_item(sym::Mutex, inner_id) { } else if tcx.is_diagnostic_item(sym::Mutex, inner_id) {
err.span_suggestion_verbose( err.span_suggestion_verbose(
expr.span.shrink_to_hi(), expr.span.shrink_to_hi(),
format!( format!(
"use `.lock()` to borrow the `{ty}`, \ "use `.lock().unwrap()` to borrow the `{ty}`, \
blocking the current thread until it can be acquired" blocking the current thread until it can be acquired"
), ),
".lock().unwrap()", ".lock().unwrap()",
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} else if tcx.is_diagnostic_item(sym::RwLock, inner_id) { } else if tcx.is_diagnostic_item(sym::RwLock, inner_id) {
match mutable { let (suggestion, borrow_kind) = match mutable {
Some(Mutability::Not) => { Some(Mutability::Not) => (".read().unwrap()", "borrow"),
err.span_suggestion_verbose( Some(Mutability::Mut) => (".write().unwrap()", "mutably borrow"),
expr.span.shrink_to_hi(),
format!(
"use `.read()` to borrow the `{ty}`, \
blocking the current thread until it can be acquired"
),
".read().unwrap()",
Applicability::MaybeIncorrect,
);
}
Some(Mutability::Mut) => {
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `.write()` to mutably borrow the `{ty}`, \
blocking the current thread until it can be acquired"
),
".write().unwrap()",
Applicability::MaybeIncorrect,
);
}
None => return, None => return,
} };
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `{suggestion}` to {borrow_kind} the `{ty}`, \
blocking the current thread until it can be acquired"
),
suggestion,
Applicability::MaybeIncorrect,
);
} else { } else {
return; return;
}; };

View file

@ -16,25 +16,25 @@ fn main() {
other_item.borrow().method(); other_item.borrow().method();
//~^ ERROR no method named `method` found for struct `RefCell` in the current scope [E0599] //~^ ERROR no method named `method` found for struct `RefCell` in the current scope [E0599]
//~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist. //~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if a mutable borrow exists
other_item.borrow_mut().some_mutable_method(); other_item.borrow_mut().some_mutable_method();
//~^ ERROR no method named `some_mutable_method` found for struct `RefCell` in the current scope [E0599] //~^ ERROR no method named `some_mutable_method` found for struct `RefCell` in the current scope [E0599]
//~| HELP use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist. //~| HELP .borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any borrows exist
let another_item = std::sync::Mutex::new(Struct { p: 42_u32 }); let another_item = std::sync::Mutex::new(Struct { p: 42_u32 });
another_item.lock().unwrap().method(); another_item.lock().unwrap().method();
//~^ ERROR no method named `method` found for struct `Mutex` in the current scope [E0599] //~^ ERROR no method named `method` found for struct `Mutex` in the current scope [E0599]
//~| HELP use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired //~| HELP use `.lock().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
let another_item = std::sync::RwLock::new(Struct { p: 42_u32 }); let another_item = std::sync::RwLock::new(Struct { p: 42_u32 });
another_item.read().unwrap().method(); another_item.read().unwrap().method();
//~^ ERROR no method named `method` found for struct `RwLock` in the current scope [E0599] //~^ ERROR no method named `method` found for struct `RwLock` in the current scope [E0599]
//~| HELP use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired //~| HELP use `.read().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
another_item.write().unwrap().some_mutable_method(); another_item.write().unwrap().some_mutable_method();
//~^ ERROR no method named `some_mutable_method` found for struct `RwLock` in the current scope [E0599] //~^ ERROR no method named `some_mutable_method` found for struct `RwLock` in the current scope [E0599]
//~| HELP use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired //~| HELP use `.write().unwrap()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
} }

View file

@ -16,25 +16,25 @@ fn main() {
other_item.method(); other_item.method();
//~^ ERROR no method named `method` found for struct `RefCell` in the current scope [E0599] //~^ ERROR no method named `method` found for struct `RefCell` in the current scope [E0599]
//~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist. //~| HELP use `.borrow()` to borrow the `Struct<u32>`, panicking if a mutable borrow exists
other_item.some_mutable_method(); other_item.some_mutable_method();
//~^ ERROR no method named `some_mutable_method` found for struct `RefCell` in the current scope [E0599] //~^ ERROR no method named `some_mutable_method` found for struct `RefCell` in the current scope [E0599]
//~| HELP use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist. //~| HELP .borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any borrows exist
let another_item = std::sync::Mutex::new(Struct { p: 42_u32 }); let another_item = std::sync::Mutex::new(Struct { p: 42_u32 });
another_item.method(); another_item.method();
//~^ ERROR no method named `method` found for struct `Mutex` in the current scope [E0599] //~^ ERROR no method named `method` found for struct `Mutex` in the current scope [E0599]
//~| HELP use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired //~| HELP use `.lock().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
let another_item = std::sync::RwLock::new(Struct { p: 42_u32 }); let another_item = std::sync::RwLock::new(Struct { p: 42_u32 });
another_item.method(); another_item.method();
//~^ ERROR no method named `method` found for struct `RwLock` in the current scope [E0599] //~^ ERROR no method named `method` found for struct `RwLock` in the current scope [E0599]
//~| HELP use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired //~| HELP use `.read().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
another_item.some_mutable_method(); another_item.some_mutable_method();
//~^ ERROR no method named `some_mutable_method` found for struct `RwLock` in the current scope [E0599] //~^ ERROR no method named `some_mutable_method` found for struct `RwLock` in the current scope [E0599]
//~| HELP use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired //~| HELP use `.write().unwrap()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
} }

View file

@ -9,7 +9,7 @@ note: the method `method` exists on the type `Struct<u32>`
| |
LL | pub fn method(&self) {} LL | pub fn method(&self) {}
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
help: use `.borrow()` to borrow the `Struct<u32>`, panicking if any outstanding mutable borrows exist. help: use `.borrow()` to borrow the `Struct<u32>`, panicking if a mutable borrow exists
| |
LL | other_item.borrow().method(); LL | other_item.borrow().method();
| +++++++++ | +++++++++
@ -25,7 +25,7 @@ note: the method `some_mutable_method` exists on the type `Struct<u32>`
| |
LL | pub fn some_mutable_method(&mut self) {} LL | pub fn some_mutable_method(&mut self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any outstanding borrows exist. help: use `.borrow_mut()` to mutably borrow the `Struct<u32>`, panicking if any borrows exist
| |
LL | other_item.borrow_mut().some_mutable_method(); LL | other_item.borrow_mut().some_mutable_method();
| +++++++++++++ | +++++++++++++
@ -41,7 +41,7 @@ note: the method `method` exists on the type `Struct<u32>`
| |
LL | pub fn method(&self) {} LL | pub fn method(&self) {}
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
help: use `.lock()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired help: use `.lock().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
| |
LL | another_item.lock().unwrap().method(); LL | another_item.lock().unwrap().method();
| ++++++++++++++++ | ++++++++++++++++
@ -57,7 +57,7 @@ note: the method `method` exists on the type `Struct<u32>`
| |
LL | pub fn method(&self) {} LL | pub fn method(&self) {}
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
help: use `.read()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired help: use `.read().unwrap()` to borrow the `Struct<u32>`, blocking the current thread until it can be acquired
| |
LL | another_item.read().unwrap().method(); LL | another_item.read().unwrap().method();
| ++++++++++++++++ | ++++++++++++++++
@ -73,7 +73,7 @@ note: the method `some_mutable_method` exists on the type `Struct<u32>`
| |
LL | pub fn some_mutable_method(&mut self) {} LL | pub fn some_mutable_method(&mut self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `.write()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired help: use `.write().unwrap()` to mutably borrow the `Struct<u32>`, blocking the current thread until it can be acquired
| |
LL | another_item.write().unwrap().some_mutable_method(); LL | another_item.write().unwrap().some_mutable_method();
| +++++++++++++++++ | +++++++++++++++++