]> git.lizzy.rs Git - rust.git/blob - tests/ui/infinite_loop.rs
lint: while immutable condition: refactor to use hir::Visitor
[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 foo() -> i32 { unimplemented!() }
5
6 fn immutable_condition() {
7     // Should warn when all vars mentionned are immutable
8     let y = 0;
9     while y < 10 {
10         println!("KO - y is immutable");
11     }
12
13     let x = 0;
14     while y < 10 && x < 3 {
15         println!("KO - x and y immutable");
16     }
17
18     let cond = false;
19     while !cond {
20         println!("KO - cond immutable");
21     }
22
23     let mut i = 0;
24     while y < 10 && i < 3 {
25         i += 1;
26         println!("OK - i is mutable");
27     }
28
29     let mut mut_cond = false;
30     while !mut_cond || cond {
31         mut_cond = true;
32         println!("OK - mut_cond is mutable");
33     }
34
35     while foo() < x {
36         println!("OK - Fn call results may vary");
37     }
38
39 }
40
41 fn unused_var() {
42     // Should warn when a (mutable) var is not used in while body
43     let (mut i, mut j) = (0, 0);
44
45     while i < 3 {
46         j = 3;
47         println!("KO - i not mentionned");
48     }
49
50     while i < 3 && j > 0 {
51         println!("KO - i and j not mentionned");
52     }
53
54     while i < 3 {
55         let mut i = 5;
56         fn_mutref(&mut i);
57         println!("KO - shadowed");
58     }
59
60     while i < 3 && j > 0 {
61         i = 5;
62         println!("OK - i in cond and mentionned");
63     }
64 }
65
66 fn used_immutable() {
67     let mut i = 0;
68
69     while i < 3 {
70         fn_constref(&i);
71         println!("KO - const reference");
72     }
73
74     while i < 3 {
75         fn_val(i);
76         println!("KO - passed by value");
77     }
78
79     while i < 3 {
80         println!("OK - passed by mutable reference");
81         fn_mutref(&mut i)
82     }
83 }
84
85 fn main() {
86     immutable_condition();
87     unused_var();
88     used_immutable();
89 }