Print 0 when end and offset is 0, and also simplify the suggestion

This commit is contained in:
rail 2020-04-27 17:51:01 +12:00
parent ad9ad6f402
commit 37261a904c
2 changed files with 14 additions and 5 deletions

View file

@ -959,7 +959,7 @@ fn detect_manual_memcpy<'a, 'tcx>(
if let PatKind::Binding(_, canonical_id, _, _) = pat.kind {
let print_sum = |arg1: &Offset, arg2: &Offset| -> String {
match (&arg1.value[..], arg1.negate, &arg2.value[..], arg2.negate) {
("0", _, "0", _) => "".into(),
("0", _, "0", _) => "0".into(),
("0", _, x, false) | (x, false, "0", _) => x.into(),
("0", _, x, true) => format!("-{}", x),
(x, false, y, false) => format!("({} + {})", x, y),
@ -981,6 +981,15 @@ fn detect_manual_memcpy<'a, 'tcx>(
}
};
let print_offset = |start_str: &Offset, inline_offset: &Offset| -> String {
let offset = print_sum(start_str, inline_offset);
if offset.as_str() == "0" {
"".into()
} else {
offset
}
};
let print_limit = |end: &Option<&Expr<'_>>, offset: Offset, var_name: &str| {
if let Some(end) = *end {
if_chain! {
@ -1020,9 +1029,9 @@ fn detect_manual_memcpy<'a, 'tcx>(
.into_iter()
.map(|(dst_var, src_var)| {
let start_str = Offset::positive(snippet(cx, start.span, "").to_string());
let dst_offset = print_sum(&start_str, &dst_var.offset);
let dst_offset = print_offset(&start_str, &dst_var.offset);
let dst_limit = print_limit(end, dst_var.offset, &dst_var.var_name);
let src_offset = print_sum(&start_str, &src_var.offset);
let src_offset = print_offset(&start_str, &src_var.offset);
let src_limit = print_limit(end, src_var.offset, &src_var.var_name);
let dst = if dst_offset == "" && dst_limit == "" {
dst_var.var_name

View file

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