From 48ed3c058f0e24dab66ce486d0080e96634529c6 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Sat, 16 Sep 2017 19:17:22 -0400 Subject: [PATCH] Extend MANUAL_MEMCPY lint so that it also detects manual clones between slices --- clippy_lints/src/loops.rs | 19 ++++++++++++++++++- tests/ui/for_loop.stderr | 10 +++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 260f9b888..5beb7eab7 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -718,6 +718,23 @@ fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr, var: } } +fn fetch_cloned_fixed_offset_var<'a, 'tcx>( + cx: &LateContext<'a, 'tcx>, + expr: &Expr, + var: ast::NodeId, +) -> Option { + if_let_chain! {[ + let ExprMethodCall(ref method, _, ref args) = expr.node, + method.name == "clone", + args.len() == 1, + let Some(arg) = args.get(0), + ], { + return get_fixed_offset_var(cx, arg, var); + }} + + get_fixed_offset_var(cx, expr, var) +} + fn get_indexed_assignments<'a, 'tcx>( cx: &LateContext<'a, 'tcx>, body: &Expr, @@ -729,7 +746,7 @@ fn get_indexed_assignments<'a, 'tcx>( var: ast::NodeId, ) -> Option<(FixedOffsetVar, FixedOffsetVar)> { if let Expr_::ExprAssign(ref lhs, ref rhs) = e.node { - match (get_fixed_offset_var(cx, lhs, var), get_fixed_offset_var(cx, rhs, var)) { + match (get_fixed_offset_var(cx, lhs, var), fetch_cloned_fixed_offset_var(cx, rhs, var)) { (Some(offset_left), Some(offset_right)) => Some((offset_left, offset_right)), _ => None, } diff --git a/tests/ui/for_loop.stderr b/tests/ui/for_loop.stderr index 721b2833d..79c6c781a 100644 --- a/tests/ui/for_loop.stderr +++ b/tests/ui/for_loop.stderr @@ -578,5 +578,13 @@ error: it looks like you're manually copying between slices 522 | | } | |_____^ help: try replacing the loop by: `dst_vec[..src_vec.len()].clone_from_slice(&src_vec[..])` -error: aborting due to 58 previous errors +error: it looks like you're manually copying between slices + --> $DIR/for_loop.rs:547:5 + | +547 | / for i in 0..src.len() { +548 | | dst[i] = src[i].clone(); +549 | | } + | |_____^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])` + +error: aborting due to 59 previous errors