Skip to content

Commit 327d995

Browse files
authored
Merge pull request #2147 from clippered/fix-manual-memcpy-on-overlapping-slices
Fix #2123 : check that the source and destination are different for m…
2 parents e2429f0 + dfa4cb7 commit 327d995

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clippy_lints/src/loops.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,14 @@ fn get_indexed_assignments<'a, 'tcx>(
749749
) -> Option<(FixedOffsetVar, FixedOffsetVar)> {
750750
if let Expr_::ExprAssign(ref lhs, ref rhs) = e.node {
751751
match (get_fixed_offset_var(cx, lhs, var), fetch_cloned_fixed_offset_var(cx, rhs, var)) {
752-
(Some(offset_left), Some(offset_right)) => Some((offset_left, offset_right)),
752+
(Some(offset_left), Some(offset_right)) => {
753+
// Source and destination must be different
754+
if offset_left.var_name != offset_right.var_name {
755+
Some((offset_left, offset_right))
756+
} else {
757+
None
758+
}
759+
},
753760
_ => None,
754761
}
755762
} else {

tests/ui/for_loop.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,11 @@ pub fn manual_clone(src: &[String], dst: &mut [String]) {
548548
dst[i] = src[i].clone();
549549
}
550550
}
551+
552+
#[warn(needless_range_loop)]
553+
pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
554+
// Same source and destination - don't trigger lint
555+
for i in 0..dst.len() {
556+
dst[d + i] = dst[s + i];
557+
}
558+
}

0 commit comments

Comments
 (0)