]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/explicit_counter_loop.rs
Rollup merge of #71633 - a1phyr:infallible_error, r=dtolnay
[rust.git] / src / tools / clippy / 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             if ch == 'a' {
42                 continue;
43             }
44             count += 1;
45             println!("{}", count);
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             if ch == 'a' {
53                 count += 1;
54             }
55             println!("{}", count);
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             count += 1;
63             if ch == 'a' {
64                 continue;
65             }
66             println!("{}", count);
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             count += 1;
74             for i in 0..2 {
75                 let _ = 123;
76             }
77             println!("{}", count);
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             count += 1;
85             for i in 0..2 {
86                 count += 1;
87             }
88             println!("{}", count);
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             while erasures.contains(&(i + skips)) {
100                 skips += 1;
101             }
102             println!("{}", skips);
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             let mut j = 0;
109             while j < 5 {
110                 skips += 1;
111                 j += 1;
112             }
113             println!("{}", skips);
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             for j in 0..5 {
120                 skips += 1;
121             }
122             println!("{}", skips);
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 }