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