Skip to content

Commit 37261a9

Browse files
committed
Print 0 when end and offset is 0, and also simplify the suggestion
1 parent ad9ad6f commit 37261a9

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

clippy_lints/src/loops.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ fn detect_manual_memcpy<'a, 'tcx>(
959959
if let PatKind::Binding(_, canonical_id, _, _) = pat.kind {
960960
let print_sum = |arg1: &Offset, arg2: &Offset| -> String {
961961
match (&arg1.value[..], arg1.negate, &arg2.value[..], arg2.negate) {
962-
("0", _, "0", _) => "".into(),
962+
("0", _, "0", _) => "0".into(),
963963
("0", _, x, false) | (x, false, "0", _) => x.into(),
964964
("0", _, x, true) => format!("-{}", x),
965965
(x, false, y, false) => format!("({} + {})", x, y),
@@ -981,6 +981,15 @@ fn detect_manual_memcpy<'a, 'tcx>(
981981
}
982982
};
983983

984+
let print_offset = |start_str: &Offset, inline_offset: &Offset| -> String {
985+
let offset = print_sum(start_str, inline_offset);
986+
if offset.as_str() == "0" {
987+
"".into()
988+
} else {
989+
offset
990+
}
991+
};
992+
984993
let print_limit = |end: &Option<&Expr<'_>>, offset: Offset, var_name: &str| {
985994
if let Some(end) = *end {
986995
if_chain! {
@@ -1020,9 +1029,9 @@ fn detect_manual_memcpy<'a, 'tcx>(
10201029
.into_iter()
10211030
.map(|(dst_var, src_var)| {
10221031
let start_str = Offset::positive(snippet(cx, start.span, "").to_string());
1023-
let dst_offset = print_sum(&start_str, &dst_var.offset);
1032+
let dst_offset = print_offset(&start_str, &dst_var.offset);
10241033
let dst_limit = print_limit(end, dst_var.offset, &dst_var.var_name);
1025-
let src_offset = print_sum(&start_str, &src_var.offset);
1034+
let src_offset = print_offset(&start_str, &src_var.offset);
10261035
let src_limit = print_limit(end, src_var.offset, &src_var.var_name);
10271036
let dst = if dst_offset == "" && dst_limit == "" {
10281037
dst_var.var_name

tests/ui/manual_memcpy.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ error: it looks like you're manually copying between slices
5858
--> $DIR/manual_memcpy.rs:94:14
5959
|
6060
LL | for i in from..from + src.len() {
61-
| ^^^^^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + src.len()].clone_from_slice(&src[0..(from + src.len() - from)])`
61+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + src.len()].clone_from_slice(&src[..(from + src.len() - from)])`
6262

6363
error: it looks like you're manually copying between slices
6464
--> $DIR/manual_memcpy.rs:98:14
6565
|
6666
LL | for i in from..from + 3 {
67-
| ^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + 3].clone_from_slice(&src[0..(from + 3 - from)])`
67+
| ^^^^^^^^^^^^^^ 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
7070
--> $DIR/manual_memcpy.rs:105:14

0 commit comments

Comments
 (0)