about summary refs log tree commit diff
path: root/tests/ui/lint/static-mut-refs.rs
blob: d2511fb5b128a175b4c3c5873bb573cd3fcea3aa (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
// Test `static_mut_refs` lint.

//@ revisions: e2021 e2024

//@ [e2021] edition:2021
//@ [e2021] run-pass

//@ [e2024] edition:2024

static mut FOO: (u32, u32) = (1, 2);

macro_rules! bar {
    ($x:expr) => {
        &mut ($x.0)
        //[e2021]~^ WARN creating a mutable reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR creating a mutable reference to mutable static [static_mut_refs]
    };
}

static mut STATIC: i64 = 1;

fn main() {
    static mut X: i32 = 1;

    static mut Y: i32 = 1;

    struct TheStruct {
        pub value: i32,
    }
    struct MyStruct {
        pub value: i32,
        pub s: TheStruct,
    }

    static mut A: MyStruct = MyStruct { value: 1, s: TheStruct { value: 2 } };

    unsafe {
        let _y = &X;
        //[e2021]~^ WARN shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs]

        let _y = &mut X;
        //[e2021]~^ WARN mutable reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR mutable reference to mutable static [static_mut_refs]

        let _z = &raw mut X;

        let _p = &raw const X;

        let ref _a = X;
        //[e2021]~^ WARN shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs]

        let (_b, _c) = (&X, &Y);
        //[e2021]~^ WARN shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs]
        //[e2021]~^^^ WARN shared reference to mutable static [static_mut_refs]
        //[e2024]~^^^^ ERROR shared reference to mutable static [static_mut_refs]

        foo(&X);
        //[e2021]~^ WARN shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs]

        static mut Z: &[i32; 3] = &[0, 1, 2];

        let _ = Z.len();
        //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

        let _ = Z[0];

        let _ = format!("{:?}", Z);
        //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

        let _v = &A.value;
        //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

        let _s = &A.s.value;
        //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

        let ref _v = A.value;
        //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
        //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

        let _x = bar!(FOO);

        STATIC += 1;
    }
}

fn foo<'a>(_x: &'a i32) {}