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