]> git.lizzy.rs Git - rust.git/commitdiff
Extend MANUAL_MEMCPY lint so that it also detects manual clones between slices
authorMarcus Klaas <mail@marcusklaas.nl>
Sat, 16 Sep 2017 23:17:22 +0000 (19:17 -0400)
committerMarcus Klaas <mail@marcusklaas.nl>
Sat, 16 Sep 2017 23:17:22 +0000 (19:17 -0400)
clippy_lints/src/loops.rs
tests/ui/for_loop.stderr

index 260f9b8884b9e29b1b5a818bb2347bedf458f888..5beb7eab71debe2881a887555015304c8484e723 100644 (file)
@@ -718,6 +718,23 @@ fn extract_offset<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, e: &Expr, var: ast::Node
     }
 }
 
+fn fetch_cloned_fixed_offset_var<'a, 'tcx>(
+    cx: &LateContext<'a, 'tcx>,
+    expr: &Expr,
+    var: ast::NodeId,
+) -> Option<FixedOffsetVar> {
+    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_assignment<'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,
             }
index 721b2833dec26307622b3816bbf29a70d70a1b34..79c6c781a7a5aae16a1b7b4069497d2b0c004dde 100644 (file)
@@ -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