1
Fork 0

Rollup merge of #121471 - estebank:lint-clone, r=TaKO8Ki

When encountering `<&T as Clone>::clone(x)` because `T: Clone`, suggest `#[derive(Clone)]`

CC #40699.

```
warning: call to `.clone()` on a reference in this situation does nothing
  --> $DIR/noop-method-call.rs:23:71
   |
LL |     let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
   |                                                                       ^^^^^^^^
   |
   = note: the type `PlainType<u32>` does not implement `Clone`, so calling `clone` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
help: remove this redundant call
   |
LL -     let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
LL +     let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref;
   |
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
   |
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
   |
```
This commit is contained in:
Matthias Krüger 2024-02-23 09:42:11 +01:00 committed by GitHub
commit 86727df4ed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 80 additions and 78 deletions

View file

@ -1314,6 +1314,12 @@ pub struct NoopMethodCallDiag<'a> {
pub trait_: Symbol,
#[suggestion(code = "", applicability = "machine-applicable")]
pub label: Span,
#[suggestion(
lint_derive_suggestion,
code = "#[derive(Clone)]\n",
applicability = "maybe-incorrect"
)]
pub suggest_derive: Option<Span>,
}
#[derive(LintDiagnostic)]

View file

@ -121,10 +121,20 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
let orig_ty = expr_ty.peel_refs();
if receiver_ty == expr_ty {
let suggest_derive = match orig_ty.kind() {
ty::Adt(def, _) => Some(cx.tcx.def_span(def.did()).shrink_to_lo()),
_ => None,
};
cx.emit_span_lint(
NOOP_METHOD_CALL,
span,
NoopMethodCallDiag { method: call.ident.name, orig_ty, trait_, label: span },
NoopMethodCallDiag {
method: call.ident.name,
orig_ty,
trait_,
label: span,
suggest_derive,
},
);
} else {
match name {