]> git.lizzy.rs Git - rust.git/blob - tests/ui/explicit_counter_loop.rs
Auto merge of #7956 - camsteffen:author, r=llogiq
[rust.git] / tests / ui / explicit_counter_loop.rs
1 #![warn(clippy::explicit_counter_loop)]
2
3 fn main() {
4     let mut vec = vec![1, 2, 3, 4];
5     let mut _index = 0;
6     for _v in &vec {
7         _index += 1
8     }
9
10     let mut _index = 1;
11     _index = 0;
12     for _v in &vec {
13         _index += 1
14     }
15
16     let mut _index = 0;
17     for _v in &mut vec {
18         _index += 1;
19     }
20
21     let mut _index = 0;
22     for _v in vec {
23         _index += 1;
24     }
25 }
26
27 mod issue_1219 {
28     pub fn test() {
29         // should not trigger the lint because variable is used after the loop #473
30         let vec = vec![1, 2, 3];
31         let mut index = 0;
32         for _v in &vec {
33             index += 1
34         }
35         println!("index: {}", index);
36
37         // should not trigger the lint because the count is conditional #1219
38         let text = "banana";
39         let mut count = 0;
40         for ch in text.chars() {
41             println!("{}", count);
42             if ch == 'a' {
43                 continue;
44             }
45             count += 1;
46         }
47
48         // should not trigger the lint because the count is conditional
49         let text = "banana";
50         let mut count = 0;
51         for ch in text.chars() {
52             println!("{}", count);
53             if ch == 'a' {
54                 count += 1;
55             }
56         }
57
58         // should trigger the lint because the count is not conditional
59         let text = "banana";
60         let mut count = 0;
61         for ch in text.chars() {
62             println!("{}", count);
63             count += 1;
64             if ch == 'a' {
65                 continue;
66             }
67         }
68
69         // should trigger the lint because the count is not conditional
70         let text = "banana";
71         let mut count = 0;
72         for ch in text.chars() {
73             println!("{}", count);
74             count += 1;
75             for i in 0..2 {
76                 let _ = 123;
77             }
78         }
79
80         // should not trigger the lint because the count is incremented multiple times
81         let text = "banana";
82         let mut count = 0;
83         for ch in text.chars() {
84             println!("{}", count);
85             count += 1;
86             for i in 0..2 {
87                 count += 1;
88             }
89         }
90     }
91 }
92
93 mod issue_3308 {
94     pub fn test() {
95         // should not trigger the lint because the count is incremented multiple times
96         let mut skips = 0;
97         let erasures = vec![];
98         for i in 0..10 {
99             println!("{}", skips);
100             while erasures.contains(&(i + skips)) {
101                 skips += 1;
102             }
103         }
104
105         // should not trigger the lint because the count is incremented multiple times
106         let mut skips = 0;
107         for i in 0..10 {
108             println!("{}", skips);
109             let mut j = 0;
110             while j < 5 {
111                 skips += 1;
112                 j += 1;
113             }
114         }
115
116         // should not trigger the lint because the count is incremented multiple times
117         let mut skips = 0;
118         for i in 0..10 {
119             println!("{}", skips);
120             for j in 0..5 {
121                 skips += 1;
122             }
123         }
124     }
125 }
126
127 mod issue_1670 {
128     pub fn test() {
129         let mut count = 0;
130         for _i in 3..10 {
131             count += 1;
132         }
133     }
134 }
135
136 mod issue_4732 {
137     pub fn test() {
138         let slice = &[1, 2, 3];
139         let mut index = 0;
140
141         // should not trigger the lint because the count is used after the loop
142         for _v in slice {
143             index += 1
144         }
145         let _closure = || println!("index: {}", index);
146     }
147 }
148
149 mod issue_4677 {
150     pub fn test() {
151         let slice = &[1, 2, 3];
152
153         // should not trigger the lint because the count is used after incremented
154         let mut count = 0;
155         for _i in slice {
156             count += 1;
157             println!("{}", count);
158         }
159     }
160 }
161
162 mod issue_7920 {
163     pub fn test() {
164         let slice = &[1, 2, 3];
165
166         let index_usize: usize = 0;
167         let mut idx_usize: usize = 0;
168
169         // should suggest `enumerate`
170         for _item in slice {
171             if idx_usize == index_usize {
172                 break;
173             }
174
175             idx_usize += 1;
176         }
177
178         let index_u32: u32 = 0;
179         let mut idx_u32: u32 = 0;
180
181         // should suggest `zip`
182         for _item in slice {
183             if idx_u32 == index_u32 {
184                 break;
185             }
186
187             idx_u32 += 1;
188         }
189     }
190 }