]> git.lizzy.rs Git - rust.git/blob - tests/ui/explicit_counter_loop.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / explicit_counter_loop.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::explicit_counter_loop)]
11
12 fn main() {
13     let mut vec = vec![1, 2, 3, 4];
14     let mut _index = 0;
15     for _v in &vec {
16         _index += 1
17     }
18
19     let mut _index = 1;
20     _index = 0;
21     for _v in &vec {
22         _index += 1
23     }
24 }
25
26 mod issue_1219 {
27     pub fn test() {
28         // should not trigger the lint because variable is used after the loop #473
29         let vec = vec![1, 2, 3];
30         let mut index = 0;
31         for _v in &vec {
32             index += 1
33         }
34         println!("index: {}", index);
35
36         // should not trigger the lint because the count is conditional #1219
37         let text = "banana";
38         let mut count = 0;
39         for ch in text.chars() {
40             if ch == 'a' {
41                 continue;
42             }
43             count += 1;
44             println!("{}", count);
45         }
46
47         // should not trigger the lint because the count is conditional
48         let text = "banana";
49         let mut count = 0;
50         for ch in text.chars() {
51             if ch == 'a' {
52                 count += 1;
53             }
54             println!("{}", count);
55         }
56
57         // should trigger the lint because the count is not conditional
58         let text = "banana";
59         let mut count = 0;
60         for ch in text.chars() {
61             count += 1;
62             if ch == 'a' {
63                 continue;
64             }
65             println!("{}", count);
66         }
67
68         // should trigger the lint because the count is not conditional
69         let text = "banana";
70         let mut count = 0;
71         for ch in text.chars() {
72             count += 1;
73             for i in 0..2 {
74                 let _ = 123;
75             }
76             println!("{}", count);
77         }
78
79         // should not trigger the lint because the count is incremented multiple times
80         let text = "banana";
81         let mut count = 0;
82         for ch in text.chars() {
83             count += 1;
84             for i in 0..2 {
85                 count += 1;
86             }
87             println!("{}", count);
88         }
89     }
90 }
91
92 mod issue_3308 {
93     pub fn test() {
94         // should not trigger the lint because the count is incremented multiple times
95         let mut skips = 0;
96         let erasures = vec![];
97         for i in 0..10 {
98             while erasures.contains(&(i + skips)) {
99                 skips += 1;
100             }
101             println!("{}", skips);
102         }
103
104         // should not trigger the lint because the count is incremented multiple times
105         let mut skips = 0;
106         for i in 0..10 {
107             let mut j = 0;
108             while j < 5 {
109                 skips += 1;
110                 j += 1;
111             }
112             println!("{}", skips);
113         }
114
115         // should not trigger the lint because the count is incremented multiple times
116         let mut skips = 0;
117         for i in 0..10 {
118             for j in 0..5 {
119                 skips += 1;
120             }
121             println!("{}", skips);
122         }
123     }
124 }