]> git.lizzy.rs Git - rust.git/blob - tests/ui/infinite_loop.rs
Merge pull request #2632 from phansch/fix_useless_format_false_positive
[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     let mut tup = (0, 0);
60     while tup.0 < 5 {
61         tup.0 += 1;
62         println!("OK - tup.0 gets mutated")
63     }
64 }
65
66 fn unused_var() {
67     // Should warn when a (mutable) var is not used in while body
68     let (mut i, mut j) = (0, 0);
69
70     while i < 3 {
71         j = 3;
72         println!("KO - i not mentionned");
73     }
74
75     while i < 3 && j > 0 {
76         println!("KO - i and j not mentionned");
77     }
78
79     while i < 3 {
80         let mut i = 5;
81         fn_mutref(&mut i);
82         println!("KO - shadowed");
83     }
84
85     while i < 3 && j > 0 {
86         i = 5;
87         println!("OK - i in cond and mentionned");
88     }
89 }
90
91 fn used_immutable() {
92     let mut i = 0;
93
94     while i < 3 {
95         fn_constref(&i);
96         println!("KO - const reference");
97     }
98
99     while i < 3 {
100         fn_val(i);
101         println!("KO - passed by value");
102     }
103
104     while i < 3 {
105         println!("OK - passed by mutable reference");
106         fn_mutref(&mut i)
107     }
108
109     while i < 3 {
110         fn_mutref(&mut i);
111         println!("OK - passed by mutable reference");
112     }
113 }
114
115 const N: i32 = 5;
116 const B: bool = false;
117
118 fn consts() {
119     while false {
120         println!("Constants are not linted");
121     }
122
123     while B {
124         println!("Constants are not linted");
125     }
126
127     while N > 0 {
128         println!("Constants are not linted");
129     }
130 }
131
132 use std::cell::Cell;
133
134 fn maybe_i_mutate(i: &Cell<bool>) { unimplemented!() }
135
136 fn internally_mutable() {
137     let b = Cell::new(true);
138
139     while b.get() {       // b cannot be silently coerced to `bool`
140         maybe_i_mutate(&b);
141         println!("OK - Method call within condition");
142     }
143 }
144
145 struct Counter {
146     count: usize,
147 }
148
149 impl Counter {
150     fn inc(&mut self) {
151         self.count += 1;
152     }
153
154     fn inc_n(&mut self, n: usize) {
155         while self.count < n {
156             self.inc();
157         }
158         println!("OK - self borrowed mutably");
159     }
160
161     fn print_n(&self, n: usize) {
162         while self.count < n {
163             println!("KO - {} is not mutated", self.count);
164         }
165     }
166 }
167
168 fn main() {
169     immutable_condition();
170     unused_var();
171     used_immutable();
172     internally_mutable();
173
174     let mut c = Counter { count: 0 };
175     c.inc_n(5);
176     c.print_n(2);
177 }