Do not trigger manual_memcpy for RangeTo

This commit is contained in:
rail 2020-04-27 18:04:37 +12:00
parent 37261a904c
commit 75ad839cd2
3 changed files with 30 additions and 29 deletions

View File

@ -951,7 +951,7 @@ fn detect_manual_memcpy<'a, 'tcx>(
) { ) {
if let Some(higher::Range { if let Some(higher::Range {
start: Some(start), start: Some(start),
ref end, end: Some(end),
limits, limits,
}) = higher::range(cx, arg) }) = higher::range(cx, arg)
{ {
@ -990,35 +990,31 @@ fn detect_manual_memcpy<'a, 'tcx>(
} }
}; };
let print_limit = |end: &Option<&Expr<'_>>, offset: Offset, var_name: &str| { let print_limit = |end: &Expr<'_>, offset: Offset, var_name: &str| {
if let Some(end) = *end { if_chain! {
if_chain! { if let ExprKind::MethodCall(ref method, _, ref len_args) = end.kind;
if let ExprKind::MethodCall(ref method, _, ref len_args) = end.kind; if method.ident.name == sym!(len);
if method.ident.name == sym!(len); if len_args.len() == 1;
if len_args.len() == 1; if let Some(arg) = len_args.get(0);
if let Some(arg) = len_args.get(0); if snippet(cx, arg.span, "??") == var_name;
if snippet(cx, arg.span, "??") == var_name; then {
then { return if offset.negate {
return if offset.negate { format!("({} - {})", snippet(cx, end.span, "<src>.len()"), offset.value)
format!("({} - {})", snippet(cx, end.span, "<src>.len()"), offset.value) } else {
} else { String::new()
String::new() };
};
}
} }
let end_str = match limits {
ast::RangeLimits::Closed => {
let end = sugg::Sugg::hir(cx, end, "<count>");
format!("{}", end + sugg::ONE)
},
ast::RangeLimits::HalfOpen => format!("{}", snippet(cx, end.span, "..")),
};
print_sum(&Offset::positive(end_str), &offset)
} else {
"..".into()
} }
let end_str = match limits {
ast::RangeLimits::Closed => {
let end = sugg::Sugg::hir(cx, end, "<count>");
format!("{}", end + sugg::ONE)
},
ast::RangeLimits::HalfOpen => format!("{}", snippet(cx, end.span, "..")),
};
print_sum(&Offset::positive(end_str), &offset)
}; };
// The only statements in the for loops can be indexed assignments from // The only statements in the for loops can be indexed assignments from

View File

@ -98,6 +98,11 @@ fn index(&self, _: usize) -> &i32 {
for i in from..from + 3 { for i in from..from + 3 {
dst[i] = src[i - from]; dst[i] = src[i - from];
} }
// `RangeTo` `for` loop - don't trigger lint
for i in 0.. {
dst[i] = src[i];
}
} }
#[warn(clippy::needless_range_loop, clippy::manual_memcpy)] #[warn(clippy::needless_range_loop, clippy::manual_memcpy)]

View File

@ -67,7 +67,7 @@ LL | for i in from..from + 3 {
| ^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + 3].clone_from_slice(&src[..(from + 3 - from)])` | ^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + 3].clone_from_slice(&src[..(from + 3 - from)])`
error: it looks like you're manually copying between slices error: it looks like you're manually copying between slices
--> $DIR/manual_memcpy.rs:105:14 --> $DIR/manual_memcpy.rs:110:14
| |
LL | for i in 0..src.len() { LL | for i in 0..src.len() {
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])` | ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])`