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