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.
This commit is contained in:
Shotaro Yamada 2020-03-13 00:54:40 +09:00
parent 9de642190e
commit a377378528

View File

@ -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) ||