about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/deref_addrof.rs
blob: bc253716affd6711276a0b1beb39babee33b164c (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![allow(
    dangerous_implicit_autorefs,
    clippy::explicit_auto_deref,
    clippy::return_self_not_must_use,
    clippy::useless_vec
)]
#![warn(clippy::deref_addrof)]

fn get_number() -> usize {
    10
}

fn get_reference(n: &usize) -> &usize {
    n
}

#[allow(clippy::double_parens)]
#[allow(unused_variables, unused_parens)]
fn main() {
    let a = 10;
    let aref = &a;

    let b = *&a;
    //~^ deref_addrof

    let b = *&get_number();
    //~^ deref_addrof

    let b = *get_reference(&a);

    let bytes: Vec<usize> = vec![1, 2, 3, 4];
    let b = *&bytes[1..2][0];
    //~^ deref_addrof

    //This produces a suggestion of 'let b = (a);' which
    //will trigger the 'unused_parens' lint
    let b = *&(a);
    //~^ deref_addrof

    let b = *(&a);
    //~^ deref_addrof

    #[rustfmt::skip]
    let b = *((&a));
    //~^ deref_addrof

    let b = *&&a;
    //~^ deref_addrof

    let b = **&aref;
    //~^ deref_addrof

    let _ = unsafe { *core::ptr::addr_of!(a) };

    let _repeat = *&[0; 64];
    //~^ deref_addrof
    // do NOT lint for array as semantic differences with/out `*&`.
    let _arr = *&[0, 1, 2, 3, 4];

    // Do not lint when text comes from macro
    macro_rules! mac {
        (dr) => {
            *&0
        };
        (dr $e:expr) => {
            *&$e
        };
        (r $e:expr) => {
            &$e
        };
    }
    let b = mac!(dr);
    let b = mac!(dr a);
    let b = *mac!(r a);
}

fn issue14386() {
    use std::mem::ManuallyDrop;

    #[derive(Copy, Clone)]
    struct Data {
        num: u64,
    }

    #[derive(Clone, Copy)]
    struct M {
        md: ManuallyDrop<[u8; 4]>,
    }

    union DataWithPadding<'lt> {
        data: ManuallyDrop<Data>,
        prim: ManuallyDrop<u64>,
        padding: [u8; size_of::<Data>()],
        tup: (ManuallyDrop<Data>, ()),
        indirect: M,
        indirect_arr: [M; 2],
        indirect_ref: &'lt mut M,
    }

    let mut a = DataWithPadding {
        padding: [0; size_of::<DataWithPadding>()],
    };
    unsafe {
        (*&mut a.padding) = [1; size_of::<DataWithPadding>()];
        //~^ deref_addrof
        (*&mut a.tup).1 = ();
        //~^ deref_addrof
        **&mut a.prim = 0;
        //~^ deref_addrof

        (*&mut a.data).num = 42;
        //~^ deref_addrof
        (*&mut a.indirect.md)[3] = 1;
        //~^ deref_addrof
        (*&mut a.indirect_arr[1].md)[3] = 1;
        //~^ deref_addrof
        (*&mut a.indirect_ref.md)[3] = 1;
        //~^ deref_addrof

        // Check that raw pointers are properly considered as well
        **&raw mut a.prim = 0;
        //~^ deref_addrof
        (*&raw mut a.data).num = 42;
        //~^ deref_addrof

        // Do not lint, as the dereference happens later, we cannot
        // just remove `&mut`
        (*&mut a.tup).0.num = 42;
    }
}