]> git.lizzy.rs Git - rust.git/blob - tests/ui/infinite_loop.rs
iterate List by value
[rust.git] / tests / ui / infinite_loop.rs
1 fn fn_val(i: i32) -> i32 {
2     unimplemented!()
3 }
4 fn fn_constref(i: &i32) -> i32 {
5     unimplemented!()
6 }
7 fn fn_mutref(i: &mut i32) {
8     unimplemented!()
9 }
10 fn fooi() -> i32 {
11     unimplemented!()
12 }
13 fn foob() -> bool {
14     unimplemented!()
15 }
16
17 #[allow(clippy::many_single_char_names)]
18 fn immutable_condition() {
19     // Should warn when all vars mentioned are immutable
20     let y = 0;
21     while y < 10 {
22         println!("KO - y is immutable");
23     }
24
25     let x = 0;
26     while y < 10 && x < 3 {
27         let mut k = 1;
28         k += 2;
29         println!("KO - x and y immutable");
30     }
31
32     let cond = false;
33     while !cond {
34         println!("KO - cond immutable");
35     }
36
37     let mut i = 0;
38     while y < 10 && i < 3 {
39         i += 1;
40         println!("OK - i is mutable");
41     }
42
43     let mut mut_cond = false;
44     while !mut_cond || cond {
45         mut_cond = true;
46         println!("OK - mut_cond is mutable");
47     }
48
49     while fooi() < x {
50         println!("OK - Fn call results may vary");
51     }
52
53     while foob() {
54         println!("OK - Fn call results may vary");
55     }
56
57     let mut a = 0;
58     let mut c = move || {
59         while a < 5 {
60             a += 1;
61             println!("OK - a is mutable");
62         }
63     };
64     c();
65
66     let mut tup = (0, 0);
67     while tup.0 < 5 {
68         tup.0 += 1;
69         println!("OK - tup.0 gets mutated")
70     }
71 }
72
73 fn unused_var() {
74     // Should warn when a (mutable) var is not used in while body
75     let (mut i, mut j) = (0, 0);
76
77     while i < 3 {
78         j = 3;
79         println!("KO - i not mentioned");
80     }
81
82     while i < 3 && j > 0 {
83         println!("KO - i and j not mentioned");
84     }
85
86     while i < 3 {
87         let mut i = 5;
88         fn_mutref(&mut i);
89         println!("KO - shadowed");
90     }
91
92     while i < 3 && j > 0 {
93         i = 5;
94         println!("OK - i in cond and mentioned");
95     }
96 }
97
98 fn used_immutable() {
99     let mut i = 0;
100
101     while i < 3 {
102         fn_constref(&i);
103         println!("KO - const reference");
104     }
105
106     while i < 3 {
107         fn_val(i);
108         println!("KO - passed by value");
109     }
110
111     while i < 3 {
112         println!("OK - passed by mutable reference");
113         fn_mutref(&mut i)
114     }
115
116     while i < 3 {
117         fn_mutref(&mut i);
118         println!("OK - passed by mutable reference");
119     }
120 }
121
122 const N: i32 = 5;
123 const B: bool = false;
124
125 fn consts() {
126     while false {
127         println!("Constants are not linted");
128     }
129
130     while B {
131         println!("Constants are not linted");
132     }
133
134     while N > 0 {
135         println!("Constants are not linted");
136     }
137 }
138
139 use std::cell::Cell;
140
141 fn maybe_i_mutate(i: &Cell<bool>) {
142     unimplemented!()
143 }
144
145 fn internally_mutable() {
146     let b = Cell::new(true);
147
148     while b.get() {
149         // b cannot be silently coerced to `bool`
150         maybe_i_mutate(&b);
151         println!("OK - Method call within condition");
152     }
153 }
154
155 struct Counter {
156     count: usize,
157 }
158
159 impl Counter {
160     fn inc(&mut self) {
161         self.count += 1;
162     }
163
164     fn inc_n(&mut self, n: usize) {
165         while self.count < n {
166             self.inc();
167         }
168         println!("OK - self borrowed mutably");
169     }
170
171     fn print_n(&self, n: usize) {
172         while self.count < n {
173             println!("KO - {} is not mutated", self.count);
174         }
175     }
176 }
177
178 fn while_loop_with_break_and_return() {
179     let y = 0;
180     while y < 10 {
181         if y == 0 {
182             break;
183         }
184         println!("KO - loop contains break");
185     }
186
187     while y < 10 {
188         if y == 0 {
189             return;
190         }
191         println!("KO - loop contains return");
192     }
193 }
194
195 fn main() {
196     immutable_condition();
197     unused_var();
198     used_immutable();
199     internally_mutable();
200
201     let mut c = Counter { count: 0 };
202     c.inc_n(5);
203     c.print_n(2);
204
205     while_loop_with_break_and_return();
206 }