1 #![feature(inline_const)]
2 #![allow(
3 clippy::eq_op,
4 clippy::single_match,
5 unused_assignments,
6 unused_variables,
7 clippy::while_immutable_condition
8 )]
9
test1()10 fn test1() {
11 let mut x = 0;
12 loop {
13 // clippy::never_loop
14 x += 1;
15 if x == 1 {
16 return;
17 }
18 break;
19 }
20 }
21
test2()22 fn test2() {
23 let mut x = 0;
24 loop {
25 x += 1;
26 if x == 1 {
27 break;
28 }
29 }
30 }
31
test3()32 fn test3() {
33 let mut x = 0;
34 loop {
35 // never loops
36 x += 1;
37 break;
38 }
39 }
40
test4()41 fn test4() {
42 let mut x = 1;
43 loop {
44 x += 1;
45 match x {
46 5 => return,
47 _ => (),
48 }
49 }
50 }
51
test5()52 fn test5() {
53 let i = 0;
54 loop {
55 // never loops
56 while i == 0 {
57 // never loops
58 break;
59 }
60 return;
61 }
62 }
63
test6()64 fn test6() {
65 let mut x = 0;
66 'outer: loop {
67 x += 1;
68 loop {
69 // never loops
70 if x == 5 {
71 break;
72 }
73 continue 'outer;
74 }
75 return;
76 }
77 }
78
test7()79 fn test7() {
80 let mut x = 0;
81 loop {
82 x += 1;
83 match x {
84 1 => continue,
85 _ => (),
86 }
87 return;
88 }
89 }
90
test8()91 fn test8() {
92 let mut x = 0;
93 loop {
94 x += 1;
95 match x {
96 5 => return,
97 _ => continue,
98 }
99 }
100 }
101
test9()102 fn test9() {
103 let x = Some(1);
104 while let Some(y) = x {
105 // never loops
106 return;
107 }
108 }
109
test10()110 fn test10() {
111 for x in 0..10 {
112 // never loops
113 match x {
114 1 => break,
115 _ => return,
116 }
117 }
118 }
119
test11<F: FnMut() -> i32>(mut f: F)120 fn test11<F: FnMut() -> i32>(mut f: F) {
121 loop {
122 return match f() {
123 1 => continue,
124 _ => (),
125 };
126 }
127 }
128
test12(a: bool, b: bool)129 pub fn test12(a: bool, b: bool) {
130 'label: loop {
131 loop {
132 if a {
133 continue 'label;
134 }
135 if b {
136 break;
137 }
138 }
139 break;
140 }
141 }
142
test13()143 pub fn test13() {
144 let mut a = true;
145 loop {
146 // infinite loop
147 while a {
148 if true {
149 a = false;
150 continue;
151 }
152 return;
153 }
154 }
155 }
156
test14()157 pub fn test14() {
158 let mut a = true;
159 'outer: while a {
160 // never loops
161 while a {
162 if a {
163 a = false;
164 continue;
165 }
166 }
167 break 'outer;
168 }
169 }
170
171 // Issue #1991: the outer loop should not warn.
test15()172 pub fn test15() {
173 'label: loop {
174 while false {
175 break 'label;
176 }
177 }
178 }
179
180 // Issue #4058: `continue` in `break` expression
test16()181 pub fn test16() {
182 let mut n = 1;
183 loop {
184 break if n != 5 {
185 n += 1;
186 continue;
187 };
188 }
189 }
190
191 // Issue #9001: `continue` in struct expression fields
test17()192 pub fn test17() {
193 struct Foo {
194 f: (),
195 }
196
197 let mut n = 0;
198 let _ = loop {
199 break Foo {
200 f: if n < 5 {
201 n += 1;
202 continue;
203 },
204 };
205 };
206 }
207
208 // Issue #9356: `continue` in else branch of let..else
test18()209 pub fn test18() {
210 let x = Some(0);
211 let y = 0;
212 // might loop
213 let _ = loop {
214 let Some(x) = x else {
215 if y > 0 {
216 continue;
217 } else {
218 return;
219 }
220 };
221
222 break x;
223 };
224 // never loops
225 let _ = loop {
226 let Some(x) = x else {
227 return;
228 };
229
230 break x;
231 };
232 }
233
234 // Issue #9831: unconditional break to internal labeled block
test19()235 pub fn test19() {
236 fn thing(iter: impl Iterator) {
237 for _ in iter {
238 'b: {
239 break 'b;
240 }
241 }
242 }
243 }
244
test20()245 pub fn test20() {
246 'a: loop {
247 'b: {
248 break 'b 'c: {
249 break 'a;
250 };
251 }
252 }
253 }
254
test21()255 pub fn test21() {
256 loop {
257 'a: {
258 {}
259 break 'a;
260 }
261 }
262 }
263
264 // Issue 10304: code after break from block was not considered
265 // unreachable code and was considered for further analysis of
266 // whether the loop would ever be executed or not.
test22()267 pub fn test22() {
268 for _ in 0..10 {
269 'block: {
270 break 'block;
271 return;
272 }
273 println!("looped");
274 }
275 }
276
test23()277 pub fn test23() {
278 for _ in 0..10 {
279 'block: {
280 for _ in 0..20 {
281 break 'block;
282 }
283 }
284 println!("looped");
285 }
286 }
287
test24()288 pub fn test24() {
289 'a: for _ in 0..10 {
290 'b: {
291 let x = Some(1);
292 match x {
293 None => break 'a,
294 Some(_) => break 'b,
295 }
296 }
297 }
298 }
299
300 // Do not lint, we can evaluate `true` to always succeed thus can short-circuit before the `return`
test25()301 pub fn test25() {
302 loop {
303 'label: {
304 if const { true } {
305 break 'label;
306 }
307 return;
308 }
309 }
310 }
311
test26()312 pub fn test26() {
313 loop {
314 'label: {
315 if 1 == 1 {
316 break 'label;
317 }
318 return;
319 }
320 }
321 }
322
test27()323 pub fn test27() {
324 loop {
325 'label: {
326 let x = true;
327 // Lints because we cannot prove it's always `true`
328 if x {
329 break 'label;
330 }
331 return;
332 }
333 }
334 }
335
main()336 fn main() {
337 test1();
338 test2();
339 test3();
340 test4();
341 test5();
342 test6();
343 test7();
344 test8();
345 test9();
346 test10();
347 test11(|| 0);
348 test12(true, false);
349 test13();
350 test14();
351 }
352