From 99eec3f54fb3c77db580699a40dc428e56942b40 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Wed, 8 Jan 2020 13:59:58 +0900 Subject: [PATCH] Fix `redundant_clone` --- clippy_lints/src/redundant_clone.rs | 7 +++++++ tests/ui/redundant_clone.fixed | 4 ++-- tests/ui/redundant_clone.stderr | 26 +++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/redundant_clone.rs b/clippy_lints/src/redundant_clone.rs index 12c99bc8f9bb..2b475efbd763 100644 --- a/clippy_lints/src/redundant_clone.rs +++ b/clippy_lints/src/redundant_clone.rs @@ -307,6 +307,13 @@ fn find_stmt_assigns_to<'tcx>( (true, mir::Rvalue::Ref(_, _, place)) | (false, mir::Rvalue::Use(mir::Operand::Copy(place))) => { base_local_and_movability(cx, mir, place) }, + (false, mir::Rvalue::Ref(_, _, place)) => { + if let [mir::ProjectionElem::Deref] = place.as_ref().projection { + base_local_and_movability(cx, mir, place) + } else { + None + } + }, _ => None, } } diff --git a/tests/ui/redundant_clone.fixed b/tests/ui/redundant_clone.fixed index e5e706e8483e..84931f66fa8d 100644 --- a/tests/ui/redundant_clone.fixed +++ b/tests/ui/redundant_clone.fixed @@ -18,11 +18,11 @@ fn main() { let _s = Path::new("/a/b/").join("c"); - let _s = Path::new("/a/b/").join("c").to_path_buf(); + let _s = Path::new("/a/b/").join("c"); let _s = OsString::new(); - let _s = OsString::new().to_os_string(); + let _s = OsString::new(); // Check that lint level works #[allow(clippy::redundant_clone)] diff --git a/tests/ui/redundant_clone.stderr b/tests/ui/redundant_clone.stderr index 62f4ce7645ed..0f185cda0197 100644 --- a/tests/ui/redundant_clone.stderr +++ b/tests/ui/redundant_clone.stderr @@ -59,6 +59,18 @@ note: this value is dropped without further use LL | let _s = Path::new("/a/b/").join("c").to_owned(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: redundant clone + --> $DIR/redundant_clone.rs:21:42 + | +LL | let _s = Path::new("/a/b/").join("c").to_path_buf(); + | ^^^^^^^^^^^^^^ help: remove this + | +note: this value is dropped without further use + --> $DIR/redundant_clone.rs:21:14 + | +LL | let _s = Path::new("/a/b/").join("c").to_path_buf(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: redundant clone --> $DIR/redundant_clone.rs:23:29 | @@ -71,6 +83,18 @@ note: this value is dropped without further use LL | let _s = OsString::new().to_owned(); | ^^^^^^^^^^^^^^^ +error: redundant clone + --> $DIR/redundant_clone.rs:25:29 + | +LL | let _s = OsString::new().to_os_string(); + | ^^^^^^^^^^^^^^^ help: remove this + | +note: this value is dropped without further use + --> $DIR/redundant_clone.rs:25:14 + | +LL | let _s = OsString::new().to_os_string(); + | ^^^^^^^^^^^^^^^ + error: redundant clone --> $DIR/redundant_clone.rs:32:19 | @@ -131,5 +155,5 @@ note: this value is dropped without further use LL | let _f = f.clone(); | ^ -error: aborting due to 11 previous errors +error: aborting due to 13 previous errors