]> git.lizzy.rs Git - rust.git/blob - tests/ui/eval_order_dependence.rs
rustup and compile-fail -> ui test move
[rust.git] / tests / ui / eval_order_dependence.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 #[deny(eval_order_dependence)]
5 #[allow(unused_assignments, unused_variables, many_single_char_names, no_effect, dead_code, blacklisted_name)]
6 fn main() {
7     let mut x = 0;
8     let a = { x = 1; 1 } + x;
9                         //~^ ERROR unsequenced read
10
11     // Example from iss#277
12     x += { x = 20; 2 }; //~ERROR unsequenced read
13
14     // Does it work in weird places?
15     // ...in the base for a struct expression?
16     struct Foo { a: i32, b: i32 };
17     let base = Foo { a: 4, b: 5 };
18     let foo = Foo { a: x, .. { x = 6; base } };
19                     //~^ ERROR unsequenced read
20     // ...inside a closure?
21     let closure = || {
22         let mut x = 0;
23         x += { x = 20; 2 }; //~ERROR unsequenced read
24     };
25     // ...not across a closure?
26     let mut y = 0;
27     let b = (y, || { y = 1 });
28
29     // && and || evaluate left-to-right.
30     let a = { x = 1; true } && (x == 3);
31     let a = { x = 1; true } || (x == 3);
32
33     // Make sure we don't get confused by alpha conversion.
34     let a = { let mut x = 1; x = 2; 1 } + x;
35
36     // No warning if we don't read the variable...
37     x = { x = 20; 2 };
38     // ...if the assignment is in a closure...
39     let b = { || { x = 1; }; 1 } + x;
40     // ... or the access is under an address.
41     let b = ({ let p = &x; 1 }, { x = 1; x });
42
43     // Limitation: l-values other than simple variables don't trigger
44     // the warning.
45     let mut tup = (0, 0);
46     let c = { tup.0 = 1; 1 } + tup.0;
47     // Limitation: you can get away with a read under address-of.
48     let mut z = 0;
49     let b = (&{ z = x; x }, { x = 3; x });
50 }