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