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