diff options
| author | bors <bors@rust-lang.org> | 2020-04-29 20:05:18 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-04-29 20:05:18 +0000 |
| commit | 0a53ed2d8ea81d09b0ae2d6c5e206ec22a0ba46c (patch) | |
| tree | ecfeb63448f7318b4b4199766cedcb1a53c79226 | |
| parent | 28197b622611ba3a6367648974ccf59127c287bb (diff) | |
| parent | 20c069beec7cff9d77a6e119037dedb6db87edc7 (diff) | |
| download | rust-0a53ed2d8ea81d09b0ae2d6c5e206ec22a0ba46c.tar.gz rust-0a53ed2d8ea81d09b0ae2d6c5e206ec22a0ba46c.zip | |
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`]
| -rw-r--r-- | clippy_lints/src/methods/mod.rs | 4 | ||||
| -rw-r--r-- | tests/ui/unnecessary_clone.rs | 5 | ||||
| -rw-r--r-- | tests/ui/unnecessary_clone.stderr | 38 |
3 files changed, 41 insertions, 6 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 7f773c602ed..3676dc5b09d 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -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, ); diff --git a/tests/ui/unnecessary_clone.rs b/tests/ui/unnecessary_clone.rs index 7a1d031fac4..f1cc5b564c1 100644 --- a/tests/ui/unnecessary_clone.rs +++ b/tests/ui/unnecessary_clone.rs @@ -109,4 +109,9 @@ mod many_derefs { let _: E = a.clone(); let _: E = *****a; } + + fn check(mut encoded: &[u8]) { + let _ = &mut encoded.clone(); + let _ = &encoded.clone(); + } } diff --git a/tests/ui/unnecessary_clone.stderr b/tests/ui/unnecessary_clone.stderr index 7b34ff9e315..6176a2bc464 100644 --- a/tests/ui/unnecessary_clone.stderr +++ b/tests/ui/unnecessary_clone.stderr @@ -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 |
