Skip to content

Commit 75ad839

Browse files
committed
Do not trigger manual_memcpy for RangeTo
1 parent 37261a9 commit 75ad839

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

clippy_lints/src/loops.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ fn detect_manual_memcpy<'a, 'tcx>(
951951
) {
952952
if let Some(higher::Range {
953953
start: Some(start),
954-
ref end,
954+
end: Some(end),
955955
limits,
956956
}) = higher::range(cx, arg)
957957
{
@@ -990,35 +990,31 @@ fn detect_manual_memcpy<'a, 'tcx>(
990990
}
991991
};
992992

993-
let print_limit = |end: &Option<&Expr<'_>>, offset: Offset, var_name: &str| {
994-
if let Some(end) = *end {
995-
if_chain! {
996-
if let ExprKind::MethodCall(ref method, _, ref len_args) = end.kind;
997-
if method.ident.name == sym!(len);
998-
if len_args.len() == 1;
999-
if let Some(arg) = len_args.get(0);
1000-
if snippet(cx, arg.span, "??") == var_name;
1001-
then {
1002-
return if offset.negate {
1003-
format!("({} - {})", snippet(cx, end.span, "<src>.len()"), offset.value)
1004-
} else {
1005-
String::new()
1006-
};
1007-
}
993+
let print_limit = |end: &Expr<'_>, offset: Offset, var_name: &str| {
994+
if_chain! {
995+
if let ExprKind::MethodCall(ref method, _, ref len_args) = end.kind;
996+
if method.ident.name == sym!(len);
997+
if len_args.len() == 1;
998+
if let Some(arg) = len_args.get(0);
999+
if snippet(cx, arg.span, "??") == var_name;
1000+
then {
1001+
return if offset.negate {
1002+
format!("({} - {})", snippet(cx, end.span, "<src>.len()"), offset.value)
1003+
} else {
1004+
String::new()
1005+
};
10081006
}
1007+
}
10091008

1010-
let end_str = match limits {
1011-
ast::RangeLimits::Closed => {
1012-
let end = sugg::Sugg::hir(cx, end, "<count>");
1013-
format!("{}", end + sugg::ONE)
1014-
},
1015-
ast::RangeLimits::HalfOpen => format!("{}", snippet(cx, end.span, "..")),
1016-
};
1009+
let end_str = match limits {
1010+
ast::RangeLimits::Closed => {
1011+
let end = sugg::Sugg::hir(cx, end, "<count>");
1012+
format!("{}", end + sugg::ONE)
1013+
},
1014+
ast::RangeLimits::HalfOpen => format!("{}", snippet(cx, end.span, "..")),
1015+
};
10171016

1018-
print_sum(&Offset::positive(end_str), &offset)
1019-
} else {
1020-
"..".into()
1021-
}
1017+
print_sum(&Offset::positive(end_str), &offset)
10221018
};
10231019

10241020
// The only statements in the for loops can be indexed assignments from

tests/ui/manual_memcpy.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) {
9898
for i in from..from + 3 {
9999
dst[i] = src[i - from];
100100
}
101+
102+
// `RangeTo` `for` loop - don't trigger lint
103+
for i in 0.. {
104+
dst[i] = src[i];
105+
}
101106
}
102107

103108
#[warn(clippy::needless_range_loop, clippy::manual_memcpy)]

tests/ui/manual_memcpy.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ LL | for i in from..from + 3 {
6767
| ^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + 3].clone_from_slice(&src[..(from + 3 - from)])`
6868

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

0 commit comments

Comments
 (0)