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