From a3773785288e71fde264b9264a142a662aace5ee Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Fri, 13 Mar 2020 00:54:40 +0900 Subject: [PATCH] Only fires on temporaries `let y = x.clone()` cannot be turned into `let y = x` without moving x, regardless of whether `y` is consumed or not. --- clippy_lints/src/redundant_clone.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/redundant_clone.rs b/clippy_lints/src/redundant_clone.rs index b30d29116cc..7c1a7040846 100644 --- a/clippy_lints/src/redundant_clone.rs +++ b/clippy_lints/src/redundant_clone.rs @@ -192,10 +192,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone { (local, deref_clone_ret) }; - // 1. `local` cannot be moved out if it is used later. - // 2. If `ret_local` is not consumed, we can remove this `clone` call anyway. + let is_temp = mir_read_only.local_kind(ret_local) == mir::LocalKind::Temp; + + // 1. `local` can be moved out if it is not used later. + // 2. If `ret_local` is a temporary and is not consumed, we can remove this `clone` call anyway. let (used, consumed) = traversal::ReversePostorder::new(&mir, bb).skip(1).fold( - (false, false), + (false, !is_temp), |(used, consumed), (tbb, tdata)| { // Short-circuit if (used && consumed) ||