summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/mixed_read_write_in_expression.rs
blob: 6efc7657ec0c92349291ee1f8b250ab3d7d8cd01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#[warn(clippy::mixed_read_write_in_expression)]
#[allow(
    unused_assignments,
    unused_variables,
    clippy::no_effect,
    dead_code,
    clippy::disallowed_names
)]
fn main() {
    let mut x = 0;
    let a = {
        x = 1;
        1
    } + x;

    // Example from iss#277
    x += {
        x = 20;
        2
    };

    // Does it work in weird places?
    // ...in the base for a struct expression?
    struct Foo {
        a: i32,
        b: i32,
    };
    let base = Foo { a: 4, b: 5 };
    let foo = Foo {
        a: x,
        ..{
            x = 6;
            base
        }
    };
    // ...inside a closure?
    let closure = || {
        let mut x = 0;
        x += {
            x = 20;
            2
        };
    };
    // ...not across a closure?
    let mut y = 0;
    let b = (y, || y = 1);

    // && and || evaluate left-to-right.
    let a = {
        x = 1;
        true
    } && (x == 3);
    let a = {
        x = 1;
        true
    } || (x == 3);

    // Make sure we don't get confused by alpha conversion.
    let a = {
        let mut x = 1;
        x = 2;
        1
    } + x;

    // No warning if we don't read the variable...
    x = {
        x = 20;
        2
    };
    // ...if the assignment is in a closure...
    let b = {
        || {
            x = 1;
        };
        1
    } + x;
    // ... or the access is under an address.
    let b = (
        {
            let p = &x;
            1
        },
        {
            x = 1;
            x
        },
    );

    // Limitation: l-values other than simple variables don't trigger
    // the warning.
    let mut tup = (0, 0);
    let c = {
        tup.0 = 1;
        1
    } + tup.0;
    // Limitation: you can get away with a read under address-of.
    let mut z = 0;
    let b = (
        &{
            z = x;
            x
        },
        {
            x = 3;
            x
        },
    );
}

async fn issue_6925() {
    let _ = vec![async { true }.await, async { false }.await];
}