]> git.lizzy.rs Git - rust.git/blob - tests/ui/never_loop.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / never_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 #![allow(
11     clippy::single_match,
12     unused_assignments,
13     unused_variables,
14     clippy::while_immutable_condition
15 )]
16
17 fn test1() {
18     let mut x = 0;
19     loop {
20         // clippy::never_loop
21         x += 1;
22         if x == 1 {
23             return;
24         }
25         break;
26     }
27 }
28
29 fn test2() {
30     let mut x = 0;
31     loop {
32         x += 1;
33         if x == 1 {
34             break;
35         }
36     }
37 }
38
39 fn test3() {
40     let mut x = 0;
41     loop {
42         // never loops
43         x += 1;
44         break;
45     }
46 }
47
48 fn test4() {
49     let mut x = 1;
50     loop {
51         x += 1;
52         match x {
53             5 => return,
54             _ => (),
55         }
56     }
57 }
58
59 fn test5() {
60     let i = 0;
61     loop {
62         // never loops
63         while i == 0 {
64             // never loops
65             break;
66         }
67         return;
68     }
69 }
70
71 fn test6() {
72     let mut x = 0;
73     'outer: loop {
74         x += 1;
75         loop {
76             // never loops
77             if x == 5 {
78                 break;
79             }
80             continue 'outer;
81         }
82         return;
83     }
84 }
85
86 fn test7() {
87     let mut x = 0;
88     loop {
89         x += 1;
90         match x {
91             1 => continue,
92             _ => (),
93         }
94         return;
95     }
96 }
97
98 fn test8() {
99     let mut x = 0;
100     loop {
101         x += 1;
102         match x {
103             5 => return,
104             _ => continue,
105         }
106     }
107 }
108
109 fn test9() {
110     let x = Some(1);
111     while let Some(y) = x {
112         // never loops
113         return;
114     }
115 }
116
117 fn test10() {
118     for x in 0..10 {
119         // never loops
120         match x {
121             1 => break,
122             _ => return,
123         }
124     }
125 }
126
127 fn test11<F: FnMut() -> i32>(mut f: F) {
128     loop {
129         return match f() {
130             1 => continue,
131             _ => (),
132         };
133     }
134 }
135
136 pub fn test12(a: bool, b: bool) {
137     'label: loop {
138         loop {
139             if a {
140                 continue 'label;
141             }
142             if b {
143                 break;
144             }
145         }
146         break;
147     }
148 }
149
150 pub fn test13() {
151     let mut a = true;
152     loop {
153         // infinite loop
154         while a {
155             if true {
156                 a = false;
157                 continue;
158             }
159             return;
160         }
161     }
162 }
163
164 pub fn test14() {
165     let mut a = true;
166     'outer: while a {
167         // never loops
168         while a {
169             if a {
170                 a = false;
171                 continue;
172             }
173         }
174         break 'outer;
175     }
176 }
177
178 // Issue #1991: the outter loop should not warn.
179 pub fn test15() {
180     'label: loop {
181         while false {
182             break 'label;
183         }
184     }
185 }
186
187 fn main() {
188     test1();
189     test2();
190     test3();
191     test4();
192     test5();
193     test6();
194     test7();
195     test8();
196     test9();
197     test10();
198     test11(|| 0);
199     test12(true, false);
200     test13();
201     test14();
202 }