]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/manual_memcpy/with_loop_counters.rs
Merge commit 'c19edfd71a1d0ddef86c2c67fdb40718d40a72b4' into sync_cg_clif-2022-07-25
[rust.git] / src / tools / clippy / tests / ui / manual_memcpy / with_loop_counters.rs
1 #![warn(clippy::needless_range_loop, clippy::manual_memcpy)]
2
3 pub fn manual_copy_with_counters(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) {
4     let mut count = 0;
5     for i in 3..src.len() {
6         dst[i] = src[count];
7         count += 1;
8     }
9
10     let mut count = 0;
11     for i in 3..src.len() {
12         dst[count] = src[i];
13         count += 1;
14     }
15
16     let mut count = 3;
17     for i in 0..src.len() {
18         dst[count] = src[i];
19         count += 1;
20     }
21
22     let mut count = 3;
23     for i in 0..src.len() {
24         dst[i] = src[count];
25         count += 1;
26     }
27
28     let mut count = 0;
29     for i in 3..(3 + src.len()) {
30         dst[i] = src[count];
31         count += 1;
32     }
33
34     let mut count = 3;
35     for i in 5..src.len() {
36         dst[i] = src[count - 2];
37         count += 1;
38     }
39
40     let mut count = 2;
41     for i in 0..dst.len() {
42         dst[i] = src[count];
43         count += 1;
44     }
45
46     let mut count = 5;
47     for i in 3..10 {
48         dst[i] = src[count];
49         count += 1;
50     }
51
52     let mut count = 3;
53     let mut count2 = 30;
54     for i in 0..src.len() {
55         dst[count] = src[i];
56         dst2[count2] = src[i];
57         count += 1;
58         count2 += 1;
59     }
60
61     // make sure parentheses are added properly to bitwise operators, which have lower precedence than
62     // arithmetic ones
63     let mut count = 0 << 1;
64     for i in 0..1 << 1 {
65         dst[count] = src[i + 2];
66         count += 1;
67     }
68
69     // make sure incrementing expressions without semicolons at the end of loops are handled correctly.
70     let mut count = 0;
71     for i in 3..src.len() {
72         dst[i] = src[count];
73         count += 1
74     }
75
76     // make sure ones where the increment is not at the end of the loop.
77     // As a possible enhancement, one could adjust the offset in the suggestion according to
78     // the position. For example, if the increment is at the top of the loop;
79     // treating the loop counter as if it were initialized 1 greater than the original value.
80     let mut count = 0;
81     #[allow(clippy::needless_range_loop)]
82     for i in 0..src.len() {
83         count += 1;
84         dst[i] = src[count];
85     }
86 }
87
88 fn main() {}