Skip to content

Commit 1b8fa3c

Browse files
committed
Simplify manual_memcpy suggestion in some cases
1 parent 0f4b13b commit 1b8fa3c

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

clippy_lints/src/loops.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,20 @@ fn detect_manual_memcpy<'a, 'tcx>(
950950
("0", _, x, false) | (x, false, "0", false) => x.into(),
951951
("0", _, x, true) | (x, false, "0", true) => format!("-{}", x),
952952
(x, false, y, false) => format!("({} + {})", x, y),
953-
(x, false, y, true) => format!("({} - {})", x, y),
954-
(x, true, y, false) => format!("({} - {})", y, x),
953+
(x, false, y, true) => {
954+
if x == y {
955+
"0".into()
956+
} else {
957+
format!("({} - {})", x, y)
958+
}
959+
},
960+
(x, true, y, false) => {
961+
if x == y {
962+
"0".into()
963+
} else {
964+
format!("({} - {})", y, x)
965+
}
966+
},
955967
(x, true, y, true) => format!("-({} + {})", x, y),
956968
}
957969
};
@@ -1003,7 +1015,12 @@ fn detect_manual_memcpy<'a, 'tcx>(
10031015
format!("{}[{}..{}]", dst_var.var_name, dst_offset, dst_limit)
10041016
};
10051017

1006-
format!("{}.clone_from_slice(&{}[{}..{}])", dst, src_var.var_name, src_offset, src_limit)
1018+
// If the src range is simply `0..src.len()`, suggest copying the entire slice
1019+
if src_offset == "0" && src_limit == format!("{}.len()", src_var.var_name) {
1020+
format!("{}.clone_from_slice(&{})", dst, src_var.var_name)
1021+
} else {
1022+
format!("{}.clone_from_slice(&{}[{}..{}])", dst, src_var.var_name, src_offset, src_limit)
1023+
}
10071024
})
10081025
.join("\n ");
10091026

tests/ui/for_loop.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,19 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) {
550550
for i in 0..10 {
551551
dst_vec[i] = src[i];
552552
}
553+
554+
// Simplify suggestion (issue #3004)
555+
let src = [0, 1, 2, 3, 4];
556+
let mut dst = [0, 0, 0, 0, 0, 0];
557+
let from = 1;
558+
559+
for i in from..from + src.len() {
560+
dst[i] = src[i - from];
561+
}
562+
563+
for i in from..from + 3 {
564+
dst[i] = src[i - from];
565+
}
553566
}
554567

555568
#[warn(clippy::needless_range_loop)]

tests/ui/for_loop.stderr

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,22 +482,34 @@ error: it looks like you're manually copying between slices
482482
| ^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst_vec[..src_vec.len()].clone_from_slice(&src_vec[..])`
483483

484484
error: it looks like you're manually copying between slices
485-
--> $DIR/for_loop.rs:557:14
485+
--> $DIR/for_loop.rs:559:14
486486
|
487-
557 | for i in 0..src.len() {
487+
559 | for i in from..from + src.len() {
488+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + src.len()].clone_from_slice(&src[0..(from + src.len() - from)])`
489+
490+
error: it looks like you're manually copying between slices
491+
--> $DIR/for_loop.rs:563:14
492+
|
493+
563 | for i in from..from + 3 {
494+
| ^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + 3].clone_from_slice(&src[0..(from + 3 - from)])`
495+
496+
error: it looks like you're manually copying between slices
497+
--> $DIR/for_loop.rs:570:14
498+
|
499+
570 | for i in 0..src.len() {
488500
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])`
489501

490502
error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators
491-
--> $DIR/for_loop.rs:618:19
503+
--> $DIR/for_loop.rs:631:19
492504
|
493-
618 | for ch in text.chars() {
505+
631 | for ch in text.chars() {
494506
| ^^^^^^^^^^^^
495507

496508
error: the variable `count` is used as a loop counter. Consider using `for (count, item) in text.chars().enumerate()` or similar iterators
497-
--> $DIR/for_loop.rs:629:19
509+
--> $DIR/for_loop.rs:642:19
498510
|
499-
629 | for ch in text.chars() {
511+
642 | for ch in text.chars() {
500512
| ^^^^^^^^^^^^
501513

502-
error: aborting due to 61 previous errors
514+
error: aborting due to 63 previous errors
503515

tests/ui/for_loop.stdout

Whitespace-only changes.

0 commit comments

Comments
 (0)