]> git.lizzy.rs Git - rust.git/blob - tests/ui/eval_order_dependence.rs
Fix `manual_map` at the end of an if chain
[rust.git] / tests / ui / eval_order_dependence.rs
1 #[warn(clippy::eval_order_dependence)]
2 #[allow(
3     unused_assignments,
4     unused_variables,
5     clippy::many_single_char_names,
6     clippy::no_effect,
7     dead_code,
8     clippy::blacklisted_name
9 )]
10 fn main() {
11     let mut x = 0;
12     let a = {
13         x = 1;
14         1
15     } + x;
16
17     // Example from iss#277
18     x += {
19         x = 20;
20         2
21     };
22
23     // Does it work in weird places?
24     // ...in the base for a struct expression?
25     struct Foo {
26         a: i32,
27         b: i32,
28     };
29     let base = Foo { a: 4, b: 5 };
30     let foo = Foo {
31         a: x,
32         ..{
33             x = 6;
34             base
35         }
36     };
37     // ...inside a closure?
38     let closure = || {
39         let mut x = 0;
40         x += {
41             x = 20;
42             2
43         };
44     };
45     // ...not across a closure?
46     let mut y = 0;
47     let b = (y, || y = 1);
48
49     // && and || evaluate left-to-right.
50     let a = {
51         x = 1;
52         true
53     } && (x == 3);
54     let a = {
55         x = 1;
56         true
57     } || (x == 3);
58
59     // Make sure we don't get confused by alpha conversion.
60     let a = {
61         let mut x = 1;
62         x = 2;
63         1
64     } + x;
65
66     // No warning if we don't read the variable...
67     x = {
68         x = 20;
69         2
70     };
71     // ...if the assignment is in a closure...
72     let b = {
73         || {
74             x = 1;
75         };
76         1
77     } + x;
78     // ... or the access is under an address.
79     let b = (
80         {
81             let p = &x;
82             1
83         },
84         {
85             x = 1;
86             x
87         },
88     );
89
90     // Limitation: l-values other than simple variables don't trigger
91     // the warning.
92     let mut tup = (0, 0);
93     let c = {
94         tup.0 = 1;
95         1
96     } + tup.0;
97     // Limitation: you can get away with a read under address-of.
98     let mut z = 0;
99     let b = (
100         &{
101             z = x;
102             x
103         },
104         {
105             x = 3;
106             x
107         },
108     );
109 }