Skip to content

Commit e7d1808

Browse files
committed
Only check the assignment found at last
If there are more than one such assignment, the last one may be the one supplied to `clone` method. Makes `find_stmt_assigns_to` internally reverses the iterator to make the intent to "iterate statements backward" clear.
1 parent fd9f5df commit e7d1808

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

clippy_lints/src/redundant_clone.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
132132
mir,
133133
arg,
134134
from_borrow,
135-
bbdata.statements.iter().rev()
135+
bbdata.statements.iter()
136136
));
137137

138138
if from_borrow && cannot_move_out {
@@ -166,7 +166,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
166166
mir,
167167
pred_arg,
168168
true,
169-
mir[ps[0]].statements.iter().rev()
169+
mir[ps[0]].statements.iter()
170170
));
171171
if cannot_move_out {
172172
continue;
@@ -257,23 +257,29 @@ fn find_stmt_assigns_to<'a, 'tcx: 'a>(
257257
mir: &mir::Mir<'tcx>,
258258
to: mir::Local,
259259
by_ref: bool,
260-
mut stmts: impl Iterator<Item = &'a mir::Statement<'tcx>>,
260+
stmts: impl DoubleEndedIterator<Item = &'a mir::Statement<'tcx>>,
261261
) -> Option<(mir::Local, CannotMoveOut)> {
262-
stmts.find_map(|stmt| {
263-
if let mir::StatementKind::Assign(mir::Place::Local(local), v) = &stmt.kind {
264-
if *local == to {
265-
if by_ref {
266-
if let mir::Rvalue::Ref(_, _, ref place) = **v {
267-
return base_local_and_movability(cx, mir, place);
268-
}
269-
} else if let mir::Rvalue::Use(mir::Operand::Copy(ref place)) = **v {
270-
return base_local_and_movability(cx, mir, place);
262+
stmts
263+
.rev()
264+
.find_map(|stmt| {
265+
if let mir::StatementKind::Assign(mir::Place::Local(local), v) = &stmt.kind {
266+
if *local == to {
267+
return Some(v);
271268
}
272269
}
273-
}
274270

275-
None
276-
})
271+
None
272+
})
273+
.and_then(|v| {
274+
if by_ref {
275+
if let mir::Rvalue::Ref(_, _, ref place) = **v {
276+
return base_local_and_movability(cx, mir, place);
277+
}
278+
} else if let mir::Rvalue::Use(mir::Operand::Copy(ref place)) = **v {
279+
return base_local_and_movability(cx, mir, place);
280+
}
281+
None
282+
})
277283
}
278284

279285
/// Extracts and returns the undermost base `Local` of given `place`. Returns `place` itself

0 commit comments

Comments
 (0)