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