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`]
This commit is contained in:
bors 2020-04-29 20:05:18 +00:00
commit 0a53ed2d8e
3 changed files with 41 additions and 6 deletions

View File

@ -1942,7 +1942,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
}
let refs: String = iter::repeat('&').take(n + 1).collect();
let derefs: String = iter::repeat('*').take(n).collect();
let explicit = format!("{}{}::clone({})", refs, ty, snip);
let explicit = format!("<{}{}>::clone({})", refs, ty, snip);
diag.span_suggestion(
expr.span,
"try dereferencing it",
@ -1951,7 +1951,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
);
diag.span_suggestion(
expr.span,
"or try being explicit about what type to clone",
"or try being explicit if you are sure, that you want to clone a reference",
explicit,
Applicability::MaybeIncorrect,
);

View File

@ -109,4 +109,9 @@ fn go1() {
let _: E = a.clone();
let _: E = *****a;
}
fn check(mut encoded: &[u8]) {
let _ = &mut encoded.clone();
let _ = &encoded.clone();
}
}

View File

@ -85,10 +85,10 @@ help: try dereferencing it
|
LL | let z: &Vec<_> = &(*y).clone();
| ^^^^^^^^^^^^^
help: or try being explicit about what type to clone
help: or try being explicit if you are sure, that you want to clone a reference
|
LL | let z: &Vec<_> = &std::vec::Vec<i32>::clone(y);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let z: &Vec<_> = <&std::vec::Vec<i32>>::clone(y);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: using `clone` on a `Copy` type
--> $DIR/unnecessary_clone.rs:109:20
@ -96,5 +96,35 @@ error: using `clone` on a `Copy` type
LL | let _: E = a.clone();
| ^^^^^^^^^ help: try dereferencing it: `*****a`
error: aborting due to 14 previous errors
error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
--> $DIR/unnecessary_clone.rs:114:22
|
LL | let _ = &mut encoded.clone();
| ^^^^^^^^^^^^^^^
|
help: try dereferencing it
|
LL | let _ = &mut &(*encoded).clone();
| ^^^^^^^^^^^^^^^^^^^
help: or try being explicit if you are sure, that you want to clone a reference
|
LL | let _ = &mut <&[u8]>::clone(encoded);
| ^^^^^^^^^^^^^^^^^^^^^^^
error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
--> $DIR/unnecessary_clone.rs:115:18
|
LL | let _ = &encoded.clone();
| ^^^^^^^^^^^^^^^
|
help: try dereferencing it
|
LL | let _ = &&(*encoded).clone();
| ^^^^^^^^^^^^^^^^^^^
help: or try being explicit if you are sure, that you want to clone a reference
|
LL | let _ = &<&[u8]>::clone(encoded);
| ^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 16 previous errors