Skip to content

Commit a377378

Browse files
committed
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.
1 parent 9de6421 commit a377378

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

clippy_lints/src/redundant_clone.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
192192
(local, deref_clone_ret)
193193
};
194194

195-
// 1. `local` cannot be moved out if it is used later.
196-
// 2. If `ret_local` is not consumed, we can remove this `clone` call anyway.
195+
let is_temp = mir_read_only.local_kind(ret_local) == mir::LocalKind::Temp;
196+
197+
// 1. `local` can be moved out if it is not used later.
198+
// 2. If `ret_local` is a temporary and is not consumed, we can remove this `clone` call anyway.
197199
let (used, consumed) = traversal::ReversePostorder::new(&mir, bb).skip(1).fold(
198-
(false, false),
200+
(false, !is_temp),
199201
|(used, consumed), (tbb, tdata)| {
200202
// Short-circuit
201203
if (used && consumed) ||

0 commit comments

Comments
 (0)