Skip to content

Commit

Permalink
Rollup merge of #121471 - estebank:lint-clone, r=TaKO8Ki
Browse files Browse the repository at this point in the history
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);
   |
```
  • Loading branch information
matthiaskrgr committed Feb 23, 2024
2 parents 6e00f0d + 6017de4 commit 86727df
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 78 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name
lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing
.suggestion = remove this redundant call
.note = the type `{$orig_ty}` does not implement `{$trait_}`, so calling `{$method}` on `&{$orig_ty}` copies the reference, which does not do anything and can be removed
.derive_suggestion = if you meant to clone `{$orig_ty}`, implement `Clone` for it
lint_only_cast_u8_to_char = only `u8` can be cast into `char`
.suggestion = use a `char` literal instead
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_lint/src/noop_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
64 changes: 0 additions & 64 deletions tests/ui/lint/noop-method-call.fixed

This file was deleted.

1 change: 0 additions & 1 deletion tests/ui/lint/noop-method-call.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@ check-pass
//@ run-rustfix

#![feature(rustc_attrs)]
#![allow(unused)]
Expand Down
74 changes: 62 additions & 12 deletions tests/ui/lint/noop-method-call.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:16:25
--> $DIR/noop-method-call.rs:15:25
|
LL | let _ = &mut encoded.clone();
| ^^^^^^^^ help: remove this redundant call
Expand All @@ -8,52 +8,102 @@ LL | let _ = &mut encoded.clone();
= note: `#[warn(noop_method_call)]` on by default

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:18:21
--> $DIR/noop-method-call.rs:17:21
|
LL | let _ = &encoded.clone();
| ^^^^^^^^ help: remove this redundant call
|
= note: the type `[u8]` does not implement `Clone`, so calling `clone` on `&[u8]` copies the reference, which does not do anything and can be removed

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:24:71
--> $DIR/noop-method-call.rs:23:71
|
LL | let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
| ^^^^^^^^ help: remove this redundant call
| ^^^^^^^^
|
= 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);
|

warning: call to `.deref()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:32:63
--> $DIR/noop-method-call.rs:31:63
|
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
| ^^^^^^^^ help: remove this redundant call
| ^^^^^^^^
|
= note: the type `PlainType<u32>` does not implement `Deref`, so calling `deref` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
help: remove this redundant call
|
LL - let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
LL + let non_deref_type_deref: &PlainType<u32> = non_deref_type;
|
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: call to `.borrow()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:36:66
--> $DIR/noop-method-call.rs:35:66
|
LL | let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
| ^^^^^^^^^ help: remove this redundant call
| ^^^^^^^^^
|
= note: the type `PlainType<u32>` does not implement `Borrow`, so calling `borrow` on `&PlainType<u32>` copies the reference, which does not do anything and can be removed
help: remove this redundant call
|
LL - let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
LL + let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type;
|
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:45:19
--> $DIR/noop-method-call.rs:44:19
|
LL | non_clone_type.clone();
| ^^^^^^^^ help: remove this redundant call
| ^^^^^^^^
|
= note: the type `PlainType<T>` does not implement `Clone`, so calling `clone` on `&PlainType<T>` copies the reference, which does not do anything and can be removed
help: remove this redundant call
|
LL - non_clone_type.clone();
LL + non_clone_type;
|
help: if you meant to clone `PlainType<T>`, implement `Clone` for it
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: call to `.clone()` on a reference in this situation does nothing
--> $DIR/noop-method-call.rs:50:19
--> $DIR/noop-method-call.rs:49:19
|
LL | non_clone_type.clone();
| ^^^^^^^^ help: remove this redundant call
| ^^^^^^^^
|
= 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 - non_clone_type.clone();
LL + non_clone_type;
|
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
|
LL + #[derive(Clone)]
LL | struct PlainType<T>(T);
|

warning: 7 warnings emitted

0 comments on commit 86727df

Please sign in to comment.