]> git.lizzy.rs Git - rust.git/blob - tests/ui/infinite_loop.rs
Merge pull request #3265 from mikerite/fix-export
[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
11 #![feature(tool_lints)]
12
13 #![allow(clippy::trivially_copy_pass_by_ref)]
14
15
16 fn fn_val(i: i32) -> i32 { unimplemented!() }
17 fn fn_constref(i: &i32) -> i32 { unimplemented!() }
18 fn fn_mutref(i: &mut i32) { unimplemented!() }
19 fn fooi() -> i32 { unimplemented!() }
20 fn foob() -> bool { unimplemented!() }
21
22 #[allow(clippy::many_single_char_names)]
23 fn immutable_condition() {
24     // Should warn when all vars mentioned are immutable
25     let y = 0;
26     while y < 10 {
27         println!("KO - y is immutable");
28     }
29
30     let x = 0;
31     while y < 10 && x < 3 {
32         let mut k = 1;
33         k += 2;
34         println!("KO - x and y immutable");
35     }
36
37     let cond = false;
38     while !cond {
39         println!("KO - cond immutable");
40     }
41
42     let mut i = 0;
43     while y < 10 && i < 3 {
44         i += 1;
45         println!("OK - i is mutable");
46     }
47
48     let mut mut_cond = false;
49     while !mut_cond || cond {
50         mut_cond = true;
51         println!("OK - mut_cond is mutable");
52     }
53
54     while fooi() < x {
55         println!("OK - Fn call results may vary");
56     }
57
58     while foob() {
59         println!("OK - Fn call results may vary");
60     }
61
62     let mut a = 0;
63     let mut c = move || {
64         while a < 5 {
65             a += 1;
66             println!("OK - a is mutable");
67         }
68     };
69     c();
70
71     let mut tup = (0, 0);
72     while tup.0 < 5 {
73         tup.0 += 1;
74         println!("OK - tup.0 gets mutated")
75     }
76 }
77
78 fn unused_var() {
79     // Should warn when a (mutable) var is not used in while body
80     let (mut i, mut j) = (0, 0);
81
82     while i < 3 {
83         j = 3;
84         println!("KO - i not mentioned");
85     }
86
87     while i < 3 && j > 0 {
88         println!("KO - i and j not mentioned");
89     }
90
91     while i < 3 {
92         let mut i = 5;
93         fn_mutref(&mut i);
94         println!("KO - shadowed");
95     }
96
97     while i < 3 && j > 0 {
98         i = 5;
99         println!("OK - i in cond and mentioned");
100     }
101 }
102
103 fn used_immutable() {
104     let mut i = 0;
105
106     while i < 3 {
107         fn_constref(&i);
108         println!("KO - const reference");
109     }
110
111     while i < 3 {
112         fn_val(i);
113         println!("KO - passed by value");
114     }
115
116     while i < 3 {
117         println!("OK - passed by mutable reference");
118         fn_mutref(&mut i)
119     }
120
121     while i < 3 {
122         fn_mutref(&mut i);
123         println!("OK - passed by mutable reference");
124     }
125 }
126
127 const N: i32 = 5;
128 const B: bool = false;
129
130 fn consts() {
131     while false {
132         println!("Constants are not linted");
133     }
134
135     while B {
136         println!("Constants are not linted");
137     }
138
139     while N > 0 {
140         println!("Constants are not linted");
141     }
142 }
143
144 use std::cell::Cell;
145
146 fn maybe_i_mutate(i: &Cell<bool>) { unimplemented!() }
147
148 fn internally_mutable() {
149     let b = Cell::new(true);
150
151     while b.get() {       // 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 main() {
181     immutable_condition();
182     unused_var();
183     used_immutable();
184     internally_mutable();
185
186     let mut c = Counter { count: 0 };
187     c.inc_n(5);
188     c.print_n(2);
189 }