Skip to content

fix: manual_memcpy wrong indexing for multi dimensional arrays #12010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions clippy_lints/src/loops/manual_memcpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_copy;
use clippy_utils::usage::local_used_in;
use clippy_utils::{get_enclosing_block, higher, path_to_local, sugg};
use rustc_ast::ast;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -63,8 +64,9 @@ pub(super) fn check<'tcx>(
&& get_slice_like_element_ty(cx, cx.typeck_results().expr_ty(base_right)).is_some()
&& let Some((start_left, offset_left)) = get_details_from_idx(cx, idx_left, &starts)
&& let Some((start_right, offset_right)) = get_details_from_idx(cx, idx_right, &starts)

// Source and destination must be different
&& !local_used_in(cx, canonical_id, base_left)
&& !local_used_in(cx, canonical_id, base_right)
// Source and destination must be different
&& path_to_local(base_left) != path_to_local(base_right)
{
Some((
Expand Down
57 changes: 55 additions & 2 deletions tests/ui/manual_memcpy/without_loop_counters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![warn(clippy::needless_range_loop, clippy::manual_memcpy)]
#![allow(clippy::useless_vec)]
#![warn(clippy::manual_memcpy)]
#![allow(clippy::useless_vec, clippy::needless_range_loop)]
//@no-rustfix
const LOOP_OFFSET: usize = 5000;

Expand Down Expand Up @@ -158,6 +158,59 @@ pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) {
//~^ ERROR: it looks like you're manually copying between slices
dst[i] = src[i];
}

// Don't trigger lint for following multi-dimensional arrays
let src = [[0; 5]; 5];
for i in 0..4 {
dst[i] = src[i + 1][i];
}
for i in 0..5 {
dst[i] = src[i][i];
}
for i in 0..5 {
dst[i] = src[i][3];
}

let src = [0; 5];
let mut dst = [[0; 5]; 5];
for i in 0..5 {
dst[i][i] = src[i];
}

let src = [[[0; 5]; 5]; 5];
let mut dst = [0; 5];
for i in 0..5 {
dst[i] = src[i][i][i];
}
for i in 0..5 {
dst[i] = src[i][i][0];
}
for i in 0..5 {
dst[i] = src[i][0][i];
}
for i in 0..5 {
dst[i] = src[0][i][i];
}
for i in 0..5 {
dst[i] = src[0][i][1];
}
for i in 0..5 {
dst[i] = src[i][0][1];
}

// Trigger lint
let src = [[0; 5]; 5];
let mut dst = [0; 5];
for i in 0..5 {
//~^ ERROR: it looks like you're manually copying between slices
dst[i] = src[0][i];
}

let src = [[[0; 5]; 5]; 5];
for i in 0..5 {
//~^ ERROR: it looks like you're manually copying between slices
dst[i] = src[0][1][i];
}
}

#[warn(clippy::needless_range_loop, clippy::manual_memcpy)]
Expand Down
22 changes: 20 additions & 2 deletions tests/ui/manual_memcpy/without_loop_counters.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,31 @@ LL | | }
| |_____^ help: try replacing the loop by: `dst.copy_from_slice(&src);`

error: it looks like you're manually copying between slices
--> tests/ui/manual_memcpy/without_loop_counters.rs:165:5
--> tests/ui/manual_memcpy/without_loop_counters.rs:204:5
|
LL | / for i in 0..5 {
LL | |
LL | | dst[i] = src[0][i];
LL | | }
| |_____^ help: try replacing the loop by: `dst.copy_from_slice(&src[0]);`

error: it looks like you're manually copying between slices
--> tests/ui/manual_memcpy/without_loop_counters.rs:210:5
|
LL | / for i in 0..5 {
LL | |
LL | | dst[i] = src[0][1][i];
LL | | }
| |_____^ help: try replacing the loop by: `dst.copy_from_slice(&src[0][1]);`

error: it looks like you're manually copying between slices
--> tests/ui/manual_memcpy/without_loop_counters.rs:218:5
|
LL | / for i in 0..src.len() {
LL | |
LL | | dst[i] = src[i].clone();
LL | | }
| |_____^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..]);`

error: aborting due to 16 previous errors
error: aborting due to 18 previous errors