Skip to content

Commit 86727df

Browse files
authored
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); | ```
2 parents 6e00f0d + 6017de4 commit 86727df

File tree

6 files changed

+80
-78
lines changed

6 files changed

+80
-78
lines changed

compiler/rustc_lint/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ lint_non_upper_case_global = {$sort} `{$name}` should have an upper case name
429429
lint_noop_method_call = call to `.{$method}()` on a reference in this situation does nothing
430430
.suggestion = remove this redundant call
431431
.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
432+
.derive_suggestion = if you meant to clone `{$orig_ty}`, implement `Clone` for it
432433
433434
lint_only_cast_u8_to_char = only `u8` can be cast into `char`
434435
.suggestion = use a `char` literal instead

compiler/rustc_lint/src/lints.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,12 @@ pub struct NoopMethodCallDiag<'a> {
13141314
pub trait_: Symbol,
13151315
#[suggestion(code = "", applicability = "machine-applicable")]
13161316
pub label: Span,
1317+
#[suggestion(
1318+
lint_derive_suggestion,
1319+
code = "#[derive(Clone)]\n",
1320+
applicability = "maybe-incorrect"
1321+
)]
1322+
pub suggest_derive: Option<Span>,
13171323
}
13181324

13191325
#[derive(LintDiagnostic)]

compiler/rustc_lint/src/noop_method_call.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,20 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
121121
let orig_ty = expr_ty.peel_refs();
122122

123123
if receiver_ty == expr_ty {
124+
let suggest_derive = match orig_ty.kind() {
125+
ty::Adt(def, _) => Some(cx.tcx.def_span(def.did()).shrink_to_lo()),
126+
_ => None,
127+
};
124128
cx.emit_span_lint(
125129
NOOP_METHOD_CALL,
126130
span,
127-
NoopMethodCallDiag { method: call.ident.name, orig_ty, trait_, label: span },
131+
NoopMethodCallDiag {
132+
method: call.ident.name,
133+
orig_ty,
134+
trait_,
135+
label: span,
136+
suggest_derive,
137+
},
128138
);
129139
} else {
130140
match name {

tests/ui/lint/noop-method-call.fixed

-64
This file was deleted.

tests/ui/lint/noop-method-call.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ check-pass
2-
//@ run-rustfix
32

43
#![feature(rustc_attrs)]
54
#![allow(unused)]

tests/ui/lint/noop-method-call.stderr

+62-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: call to `.clone()` on a reference in this situation does nothing
2-
--> $DIR/noop-method-call.rs:16:25
2+
--> $DIR/noop-method-call.rs:15:25
33
|
44
LL | let _ = &mut encoded.clone();
55
| ^^^^^^^^ help: remove this redundant call
@@ -8,52 +8,102 @@ LL | let _ = &mut encoded.clone();
88
= note: `#[warn(noop_method_call)]` on by default
99

1010
warning: call to `.clone()` on a reference in this situation does nothing
11-
--> $DIR/noop-method-call.rs:18:21
11+
--> $DIR/noop-method-call.rs:17:21
1212
|
1313
LL | let _ = &encoded.clone();
1414
| ^^^^^^^^ help: remove this redundant call
1515
|
1616
= 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
1717

1818
warning: call to `.clone()` on a reference in this situation does nothing
19-
--> $DIR/noop-method-call.rs:24:71
19+
--> $DIR/noop-method-call.rs:23:71
2020
|
2121
LL | let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
22-
| ^^^^^^^^ help: remove this redundant call
22+
| ^^^^^^^^
2323
|
2424
= 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
25+
help: remove this redundant call
26+
|
27+
LL - let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref.clone();
28+
LL + let non_clone_type_ref_clone: &PlainType<u32> = non_clone_type_ref;
29+
|
30+
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
31+
|
32+
LL + #[derive(Clone)]
33+
LL | struct PlainType<T>(T);
34+
|
2535

2636
warning: call to `.deref()` on a reference in this situation does nothing
27-
--> $DIR/noop-method-call.rs:32:63
37+
--> $DIR/noop-method-call.rs:31:63
2838
|
2939
LL | let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
30-
| ^^^^^^^^ help: remove this redundant call
40+
| ^^^^^^^^
3141
|
3242
= 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
43+
help: remove this redundant call
44+
|
45+
LL - let non_deref_type_deref: &PlainType<u32> = non_deref_type.deref();
46+
LL + let non_deref_type_deref: &PlainType<u32> = non_deref_type;
47+
|
48+
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
49+
|
50+
LL + #[derive(Clone)]
51+
LL | struct PlainType<T>(T);
52+
|
3353

3454
warning: call to `.borrow()` on a reference in this situation does nothing
35-
--> $DIR/noop-method-call.rs:36:66
55+
--> $DIR/noop-method-call.rs:35:66
3656
|
3757
LL | let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
38-
| ^^^^^^^^^ help: remove this redundant call
58+
| ^^^^^^^^^
3959
|
4060
= 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
61+
help: remove this redundant call
62+
|
63+
LL - let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type.borrow();
64+
LL + let non_borrow_type_borrow: &PlainType<u32> = non_borrow_type;
65+
|
66+
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
67+
|
68+
LL + #[derive(Clone)]
69+
LL | struct PlainType<T>(T);
70+
|
4171

4272
warning: call to `.clone()` on a reference in this situation does nothing
43-
--> $DIR/noop-method-call.rs:45:19
73+
--> $DIR/noop-method-call.rs:44:19
4474
|
4575
LL | non_clone_type.clone();
46-
| ^^^^^^^^ help: remove this redundant call
76+
| ^^^^^^^^
4777
|
4878
= 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
79+
help: remove this redundant call
80+
|
81+
LL - non_clone_type.clone();
82+
LL + non_clone_type;
83+
|
84+
help: if you meant to clone `PlainType<T>`, implement `Clone` for it
85+
|
86+
LL + #[derive(Clone)]
87+
LL | struct PlainType<T>(T);
88+
|
4989

5090
warning: call to `.clone()` on a reference in this situation does nothing
51-
--> $DIR/noop-method-call.rs:50:19
91+
--> $DIR/noop-method-call.rs:49:19
5292
|
5393
LL | non_clone_type.clone();
54-
| ^^^^^^^^ help: remove this redundant call
94+
| ^^^^^^^^
5595
|
5696
= 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
97+
help: remove this redundant call
98+
|
99+
LL - non_clone_type.clone();
100+
LL + non_clone_type;
101+
|
102+
help: if you meant to clone `PlainType<u32>`, implement `Clone` for it
103+
|
104+
LL + #[derive(Clone)]
105+
LL | struct PlainType<T>(T);
106+
|
57107

58108
warning: 7 warnings emitted
59109

0 commit comments

Comments
 (0)