]> git.lizzy.rs Git - rust.git/blob - tests/ui/explicit_counter_loop.rs
Change explicit_counter_loop's message to add parentheses if necessary
[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
17 mod issue_1219 {
18     pub fn test() {
19         // should not trigger the lint because variable is used after the loop #473
20         let vec = vec![1, 2, 3];
21         let mut index = 0;
22         for _v in &vec {
23             index += 1
24         }
25         println!("index: {}", index);
26
27         // should not trigger the lint because the count is conditional #1219
28         let text = "banana";
29         let mut count = 0;
30         for ch in text.chars() {
31             if ch == 'a' {
32                 continue;
33             }
34             count += 1;
35             println!("{}", count);
36         }
37
38         // should not trigger the lint because the count is conditional
39         let text = "banana";
40         let mut count = 0;
41         for ch in text.chars() {
42             if ch == 'a' {
43                 count += 1;
44             }
45             println!("{}", count);
46         }
47
48         // should trigger the lint because the count is not conditional
49         let text = "banana";
50         let mut count = 0;
51         for ch in text.chars() {
52             count += 1;
53             if ch == 'a' {
54                 continue;
55             }
56             println!("{}", count);
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             count += 1;
64             for i in 0..2 {
65                 let _ = 123;
66             }
67             println!("{}", count);
68         }
69
70         // should not trigger the lint because the count is incremented multiple times
71         let text = "banana";
72         let mut count = 0;
73         for ch in text.chars() {
74             count += 1;
75             for i in 0..2 {
76                 count += 1;
77             }
78             println!("{}", count);
79         }
80     }
81 }
82
83 mod issue_3308 {
84     pub fn test() {
85         // should not trigger the lint because the count is incremented multiple times
86         let mut skips = 0;
87         let erasures = vec![];
88         for i in 0..10 {
89             while erasures.contains(&(i + skips)) {
90                 skips += 1;
91             }
92             println!("{}", skips);
93         }
94
95         // should not trigger the lint because the count is incremented multiple times
96         let mut skips = 0;
97         for i in 0..10 {
98             let mut j = 0;
99             while j < 5 {
100                 skips += 1;
101                 j += 1;
102             }
103             println!("{}", skips);
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             for j in 0..5 {
110                 skips += 1;
111             }
112             println!("{}", skips);
113         }
114     }
115 }
116
117 mod issue_1670 {
118     pub fn test() {
119         let mut count = 0;
120         for _i in 3..10 {
121             count += 1;
122         }
123     }
124 }