Suggest .clone() in some move errors

```
error[E0507]: cannot move out of `*x` which is behind a shared reference
  --> $DIR/borrowck-fn-in-const-a.rs:6:16
   |
LL |         return *x
   |                ^^ move occurs because `*x` has type `String`, which does not implement the `Copy` trait
   |
help: consider cloning the value if the performance cost is acceptable
   |
LL -         return *x
LL +         return x.clone()
   |
```
This commit is contained in:
Esteban Küber
2024-03-13 03:41:41 +00:00
parent bce78102c3
commit 10c2fbec24
35 changed files with 296 additions and 29 deletions

View File

@@ -474,6 +474,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
Some(desc) => format!("`{desc}`"),
None => "value".to_string(),
};
if let Some(expr) = self.find_expr(span) {
self.suggest_cloning(err, place_ty, expr, span);
}
err.subdiagnostic(
self.dcx(),
crate::session_diagnostics::TypeNoCopy::Label {
@@ -582,6 +587,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if binds_to.len() == 1 {
let place_desc = &format!("`{}`", self.local_names[*local].unwrap());
if let Some(expr) = self.find_expr(binding_span) {
self.suggest_cloning(err, bind_to.ty, expr, binding_span);
}
err.subdiagnostic(
self.dcx(),
crate::session_diagnostics::TypeNoCopy::Label {