Skip to content

Commit 0a53ed2

Browse files
committed
Auto merge of #5547 - CrazyRoka:fix-clone-double-ref-suggestion, r=flip1995
Fixed incorrect suggestion of `clone_double_ref` lint - Added `<_>` to suggestion - Changed help message - Added new tests Closes #5494 changelog: Improve suggestion of [`clone_double_ref`]
2 parents 28197b6 + 20c069b commit 0a53ed2

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

clippy_lints/src/methods/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
19421942
}
19431943
let refs: String = iter::repeat('&').take(n + 1).collect();
19441944
let derefs: String = iter::repeat('*').take(n).collect();
1945-
let explicit = format!("{}{}::clone({})", refs, ty, snip);
1945+
let explicit = format!("<{}{}>::clone({})", refs, ty, snip);
19461946
diag.span_suggestion(
19471947
expr.span,
19481948
"try dereferencing it",
@@ -1951,7 +1951,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
19511951
);
19521952
diag.span_suggestion(
19531953
expr.span,
1954-
"or try being explicit about what type to clone",
1954+
"or try being explicit if you are sure, that you want to clone a reference",
19551955
explicit,
19561956
Applicability::MaybeIncorrect,
19571957
);

tests/ui/unnecessary_clone.rs

+5
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ mod many_derefs {
109109
let _: E = a.clone();
110110
let _: E = *****a;
111111
}
112+
113+
fn check(mut encoded: &[u8]) {
114+
let _ = &mut encoded.clone();
115+
let _ = &encoded.clone();
116+
}
112117
}

tests/ui/unnecessary_clone.stderr

+34-4
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,46 @@ help: try dereferencing it
8585
|
8686
LL | let z: &Vec<_> = &(*y).clone();
8787
| ^^^^^^^^^^^^^
88-
help: or try being explicit about what type to clone
88+
help: or try being explicit if you are sure, that you want to clone a reference
8989
|
90-
LL | let z: &Vec<_> = &std::vec::Vec<i32>::clone(y);
91-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90+
LL | let z: &Vec<_> = <&std::vec::Vec<i32>>::clone(y);
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9292

9393
error: using `clone` on a `Copy` type
9494
--> $DIR/unnecessary_clone.rs:109:20
9595
|
9696
LL | let _: E = a.clone();
9797
| ^^^^^^^^^ help: try dereferencing it: `*****a`
9898

99-
error: aborting due to 14 previous errors
99+
error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
100+
--> $DIR/unnecessary_clone.rs:114:22
101+
|
102+
LL | let _ = &mut encoded.clone();
103+
| ^^^^^^^^^^^^^^^
104+
|
105+
help: try dereferencing it
106+
|
107+
LL | let _ = &mut &(*encoded).clone();
108+
| ^^^^^^^^^^^^^^^^^^^
109+
help: or try being explicit if you are sure, that you want to clone a reference
110+
|
111+
LL | let _ = &mut <&[u8]>::clone(encoded);
112+
| ^^^^^^^^^^^^^^^^^^^^^^^
113+
114+
error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
115+
--> $DIR/unnecessary_clone.rs:115:18
116+
|
117+
LL | let _ = &encoded.clone();
118+
| ^^^^^^^^^^^^^^^
119+
|
120+
help: try dereferencing it
121+
|
122+
LL | let _ = &&(*encoded).clone();
123+
| ^^^^^^^^^^^^^^^^^^^
124+
help: or try being explicit if you are sure, that you want to clone a reference
125+
|
126+
LL | let _ = &<&[u8]>::clone(encoded);
127+
| ^^^^^^^^^^^^^^^^^^^^^^^
128+
129+
error: aborting due to 16 previous errors
100130

0 commit comments

Comments
 (0)