Extend MANUAL_MEMCPY lint so that it also detects manual clones between slices

This commit is contained in:
Marcus Klaas 2017-09-16 19:17:22 -04:00
parent a6206cc5f8
commit 48ed3c058f
2 changed files with 27 additions and 2 deletions

View file

@ -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<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>( fn get_indexed_assignments<'a, 'tcx>(
cx: &LateContext<'a, 'tcx>, cx: &LateContext<'a, 'tcx>,
body: &Expr, body: &Expr,
@ -729,7 +746,7 @@ fn get_indexed_assignments<'a, 'tcx>(
var: ast::NodeId, var: ast::NodeId,
) -> Option<(FixedOffsetVar, FixedOffsetVar)> { ) -> Option<(FixedOffsetVar, FixedOffsetVar)> {
if let Expr_::ExprAssign(ref lhs, ref rhs) = e.node { 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)), (Some(offset_left), Some(offset_right)) => Some((offset_left, offset_right)),
_ => None, _ => None,
} }

View file

@ -578,5 +578,13 @@ error: it looks like you're manually copying between slices
522 | | } 522 | | }
| |_____^ help: try replacing the loop by: `dst_vec[..src_vec.len()].clone_from_slice(&src_vec[..])` | |_____^ 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