Skip to content

Commit 48ed3c0

Browse files
committed
Extend MANUAL_MEMCPY lint so that it also detects manual clones between slices
1 parent a6206cc commit 48ed3c0

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

clippy_lints/src/loops.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,23 @@ fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr, var:
718718
}
719719
}
720720

721+
fn fetch_cloned_fixed_offset_var<'a, 'tcx>(
722+
cx: &LateContext<'a, 'tcx>,
723+
expr: &Expr,
724+
var: ast::NodeId,
725+
) -> Option<FixedOffsetVar> {
726+
if_let_chain! {[
727+
let ExprMethodCall(ref method, _, ref args) = expr.node,
728+
method.name == "clone",
729+
args.len() == 1,
730+
let Some(arg) = args.get(0),
731+
], {
732+
return get_fixed_offset_var(cx, arg, var);
733+
}}
734+
735+
get_fixed_offset_var(cx, expr, var)
736+
}
737+
721738
fn get_indexed_assignments<'a, 'tcx>(
722739
cx: &LateContext<'a, 'tcx>,
723740
body: &Expr,
@@ -729,7 +746,7 @@ fn get_indexed_assignments<'a, 'tcx>(
729746
var: ast::NodeId,
730747
) -> Option<(FixedOffsetVar, FixedOffsetVar)> {
731748
if let Expr_::ExprAssign(ref lhs, ref rhs) = e.node {
732-
match (get_fixed_offset_var(cx, lhs, var), get_fixed_offset_var(cx, rhs, var)) {
749+
match (get_fixed_offset_var(cx, lhs, var), fetch_cloned_fixed_offset_var(cx, rhs, var)) {
733750
(Some(offset_left), Some(offset_right)) => Some((offset_left, offset_right)),
734751
_ => None,
735752
}

tests/ui/for_loop.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,5 +578,13 @@ error: it looks like you're manually copying between slices
578578
522 | | }
579579
| |_____^ help: try replacing the loop by: `dst_vec[..src_vec.len()].clone_from_slice(&src_vec[..])`
580580

581-
error: aborting due to 58 previous errors
581+
error: it looks like you're manually copying between slices
582+
--> $DIR/for_loop.rs:547:5
583+
|
584+
547 | / for i in 0..src.len() {
585+
548 | | dst[i] = src[i].clone();
586+
549 | | }
587+
| |_____^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])`
588+
589+
error: aborting due to 59 previous errors
582590

0 commit comments

Comments
 (0)