mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 23:20:39 +00:00
Extend MANUAL_MEMCPY lint so that it also detects manual clones between slices
This commit is contained in:
parent
a6206cc5f8
commit
48ed3c058f
2 changed files with 27 additions and 2 deletions
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue