Rollup merge of #114477 - estebank:arc-clone, r=compiler-errors

Account for `Rc` and `Arc` when suggesting to clone

When suggesting to clone a reference-counted value, be less uncertain.
This commit is contained in:
Matthias Krüger 2023-08-04 21:32:00 +02:00 committed by GitHub
commit 35b271306f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -751,9 +751,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
) )
.must_apply_modulo_regions() .must_apply_modulo_regions()
{ {
let msg = if let ty::Adt(def, _) = ty.kind()
&& [
tcx.get_diagnostic_item(sym::Arc),
tcx.get_diagnostic_item(sym::Rc),
].contains(&Some(def.did()))
{
"clone the value to increment its reference count"
} else {
"consider cloning the value if the performance cost is acceptable"
};
err.span_suggestion_verbose( err.span_suggestion_verbose(
span.shrink_to_hi(), span.shrink_to_hi(),
"consider cloning the value if the performance cost is acceptable", msg,
suggestion, suggestion,
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );

View File

@ -8,7 +8,7 @@ LL | (t, t)
| | | |
| value moved here | value moved here
| |
help: consider cloning the value if the performance cost is acceptable help: clone the value to increment its reference count
| |
LL | (t.clone(), t) LL | (t.clone(), t)
| ++++++++ | ++++++++