]> git.lizzy.rs Git - rust.git/blob - tests/ui/infinite_loop.rs
lint: while immutable condition: do not lint constants
[rust.git] / tests / ui / infinite_loop.rs
1 fn fn_val(i: i32) -> i32 { unimplemented!() }
2 fn fn_constref(i: &i32) -> i32 { unimplemented!() }
3 fn fn_mutref(i: &mut i32) { unimplemented!() }
4 fn fooi() -> i32 { unimplemented!() }
5 fn foob() -> bool { unimplemented!() }
6
7 fn immutable_condition() {
8     // Should warn when all vars mentionned are immutable
9     let y = 0;
10     while y < 10 {
11         println!("KO - y is immutable");
12     }
13
14     let x = 0;
15     while y < 10 && x < 3 {
16         let mut k = 1;
17         k += 2;
18         println!("KO - x and y immutable");
19     }
20
21     let cond = false;
22     while !cond {
23         println!("KO - cond immutable");
24     }
25
26     let mut i = 0;
27     while y < 10 && i < 3 {
28         i += 1;
29         println!("OK - i is mutable");
30     }
31
32     let mut mut_cond = false;
33     while !mut_cond || cond {
34         mut_cond = true;
35         println!("OK - mut_cond is mutable");
36     }
37
38     while fooi() < x {
39         println!("OK - Fn call results may vary");
40     }
41
42     while foob() {
43         println!("OK - Fn call results may vary");
44     }
45
46 }
47
48 fn unused_var() {
49     // Should warn when a (mutable) var is not used in while body
50     let (mut i, mut j) = (0, 0);
51
52     while i < 3 {
53         j = 3;
54         println!("KO - i not mentionned");
55     }
56
57     while i < 3 && j > 0 {
58         println!("KO - i and j not mentionned");
59     }
60
61     while i < 3 {
62         let mut i = 5;
63         fn_mutref(&mut i);
64         println!("KO - shadowed");
65     }
66
67     while i < 3 && j > 0 {
68         i = 5;
69         println!("OK - i in cond and mentionned");
70     }
71 }
72
73 fn used_immutable() {
74     let mut i = 0;
75
76     while i < 3 {
77         fn_constref(&i);
78         println!("KO - const reference");
79     }
80
81     while i < 3 {
82         fn_val(i);
83         println!("KO - passed by value");
84     }
85
86     while i < 3 {
87         println!("OK - passed by mutable reference");
88         fn_mutref(&mut i)
89     }
90
91     while i < 3 {
92         fn_mutref(&mut i);
93         println!("OK - passed by mutable reference");
94     }
95 }
96
97 const N: i32 = 5;
98 const B: bool = false;
99
100 fn consts() {
101     while false {
102         println!("Constants are not linted");
103     }
104
105     while B {
106         println!("Constants are not linted");
107     }
108
109     while N > 0 {
110         println!("Constants are not linted");
111     }
112 }
113
114 use std::cell::Cell;
115
116 fn maybe_i_mutate(i: &Cell<bool>) { unimplemented!() }
117
118 fn internally_mutable() {
119     let b = Cell::new(true);
120
121     while b.get() {       // b cannot be silently coerced to `bool`
122         maybe_i_mutate(&b);
123         println!("OK - Method call within condition");
124     }
125 }
126
127 fn main() {
128     immutable_condition();
129     unused_var();
130     used_immutable();
131     internally_mutable();
132 }