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