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