]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/eval_order_dependence.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / eval_order_dependence.rs
1
2
3
4 #[warn(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
10 // Example from iss#277
11 x += { x = 20; 2 };
12
13 // Does it work in weird places?
14 // ...in the base for a struct expression?
15 struct Foo { a: i32, b: i32 };
16 let base = Foo { a: 4, b: 5 };
17 let foo = Foo { a: x, .. { x = 6; base } };
18 // ...inside a closure?
19 let closure = || {
20 let mut x = 0;
21 x += { x = 20; 2 };
22 };
23 // ...not across a closure?
24 let mut y = 0;
25 let b = (y, || { y = 1 });
26
27 // && and || evaluate left-to-right.
28 let a = { x = 1; true } && (x == 3);
29 let a = { x = 1; true } || (x == 3);
30
31 // Make sure we don't get confused by alpha conversion.
32 let a = { let mut x = 1; x = 2; 1 } + x;
33
34 // No warning if we don't read the variable...
35 x = { x = 20; 2 };
36 // ...if the assignment is in a closure...
37 let b = { || { x = 1; }; 1 } + x;
38 // ... or the access is under an address.
39 let b = ({ let p = &x; 1 }, { x = 1; x });
40
41 // Limitation: l-values other than simple variables don't trigger
42 // the warning.
43 let mut tup = (0, 0);
44 let c = { tup.0 = 1; 1 } + tup.0;
45 // Limitation: you can get away with a read under address-of.
46 let mut z = 0;
47 let b = (&{ z = x; x }, { x = 3; x });
48 }