]> git.lizzy.rs Git - rust.git/blob - tests/ui/eval_order_dependence.rs
Merge branch 'master' into rustfmt_tests
[rust.git] / tests / ui / eval_order_dependence.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #[warn(clippy::eval_order_dependence)]
11 #[allow(
12     unused_assignments,
13     unused_variables,
14     clippy::many_single_char_names,
15     clippy::no_effect,
16     dead_code,
17     clippy::blacklisted_name
18 )]
19 fn main() {
20     let mut x = 0;
21     let a = {
22         x = 1;
23         1
24     } + x;
25
26     // Example from iss#277
27     x += {
28         x = 20;
29         2
30     };
31
32     // Does it work in weird places?
33     // ...in the base for a struct expression?
34     struct Foo {
35         a: i32,
36         b: i32,
37     };
38     let base = Foo { a: 4, b: 5 };
39     let foo = Foo {
40         a: x,
41         ..{
42             x = 6;
43             base
44         }
45     };
46     // ...inside a closure?
47     let closure = || {
48         let mut x = 0;
49         x += {
50             x = 20;
51             2
52         };
53     };
54     // ...not across a closure?
55     let mut y = 0;
56     let b = (y, || y = 1);
57
58     // && and || evaluate left-to-right.
59     let a = {
60         x = 1;
61         true
62     } && (x == 3);
63     let a = {
64         x = 1;
65         true
66     } || (x == 3);
67
68     // Make sure we don't get confused by alpha conversion.
69     let a = {
70         let mut x = 1;
71         x = 2;
72         1
73     } + x;
74
75     // No warning if we don't read the variable...
76     x = {
77         x = 20;
78         2
79     };
80     // ...if the assignment is in a closure...
81     let b = {
82         || {
83             x = 1;
84         };
85         1
86     } + x;
87     // ... or the access is under an address.
88     let b = (
89         {
90             let p = &x;
91             1
92         },
93         {
94             x = 1;
95             x
96         },
97     );
98
99     // Limitation: l-values other than simple variables don't trigger
100     // the warning.
101     let mut tup = (0, 0);
102     let c = {
103         tup.0 = 1;
104         1
105     } + tup.0;
106     // Limitation: you can get away with a read under address-of.
107     let mut z = 0;
108     let b = (
109         &{
110             z = x;
111             x
112         },
113         {
114             x = 3;
115             x
116         },
117     );
118 }