about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/trailing_empty_array.rs
blob: a8c3ffdb0dab3d7da0727b9104810ccfef5dd730 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#![warn(clippy::trailing_empty_array)]
#![allow(clippy::repr_packed_without_abi)]

// Do lint:

struct RarelyUseful {
    //~^ trailing_empty_array
    field: i32,
    last: [usize; 0],
}

struct OnlyField {
    //~^ trailing_empty_array
    first_and_last: [usize; 0],
}

struct GenericArrayType<T> {
    //~^ trailing_empty_array
    field: i32,
    last: [T; 0],
}

#[must_use]
struct OnlyAnotherAttribute {
    //~^ trailing_empty_array
    field: i32,
    last: [usize; 0],
}

#[derive(Debug)]
struct OnlyADeriveAttribute {
    //~^ trailing_empty_array
    field: i32,
    last: [usize; 0],
}

const ZERO: usize = 0;
struct ZeroSizedWithConst {
    //~^ trailing_empty_array
    field: i32,
    last: [usize; ZERO],
}

#[allow(clippy::eq_op)]
const fn compute_zero() -> usize {
    (4 + 6) - (2 * 5)
}
struct ZeroSizedWithConstFunction {
    //~^ trailing_empty_array
    field: i32,
    last: [usize; compute_zero()],
}

const fn compute_zero_from_arg(x: usize) -> usize {
    x - 1
}
struct ZeroSizedWithConstFunction2 {
    //~^ trailing_empty_array
    field: i32,
    last: [usize; compute_zero_from_arg(1)],
}

struct ZeroSizedArrayWrapper([usize; 0]);
//~^ trailing_empty_array

struct TupleStruct(i32, [usize; 0]);
//~^ trailing_empty_array

struct LotsOfFields {
    //~^ trailing_empty_array
    f1: u32,
    f2: u32,
    f3: u32,
    f4: u32,
    f5: u32,
    f6: u32,
    f7: u32,
    f8: u32,
    f9: u32,
    f10: u32,
    f11: u32,
    f12: u32,
    f13: u32,
    f14: u32,
    f15: u32,
    f16: u32,
    last: [usize; 0],
}

// Don't lint

#[repr(C)]
struct GoodReason {
    field: i32,
    last: [usize; 0],
}

#[repr(C)]
struct OnlyFieldWithReprC {
    first_and_last: [usize; 0],
}

struct NonZeroSizedArray {
    field: i32,
    last: [usize; 1],
}

struct NotLastField {
    f1: u32,
    zero_sized: [usize; 0],
    last: i32,
}

const ONE: usize = 1;
struct NonZeroSizedWithConst {
    field: i32,
    last: [usize; ONE],
}

#[derive(Debug)]
#[repr(C)]
struct AlsoADeriveAttribute {
    field: i32,
    last: [usize; 0],
}

#[must_use]
#[repr(C)]
struct AlsoAnotherAttribute {
    field: i32,
    last: [usize; 0],
}

#[repr(packed)]
struct ReprPacked {
    field: i32,
    last: [usize; 0],
}

#[repr(C, packed)]
struct ReprCPacked {
    field: i32,
    last: [usize; 0],
}

#[repr(align(64))]
struct ReprAlign {
    field: i32,
    last: [usize; 0],
}
#[repr(C, align(64))]
struct ReprCAlign {
    field: i32,
    last: [usize; 0],
}

// NOTE: because of https://doc.rust-lang.org/stable/reference/type-layout.html#primitive-representation-of-enums-with-fields and I'm not sure when in the compilation pipeline that would happen
#[repr(C)]
enum DontLintAnonymousStructsFromDesugaring {
    A(u32),
    B(f32, [u64; 0]),
    C { x: u32, y: [u64; 0] },
}

#[repr(C)]
struct TupleStructReprC(i32, [usize; 0]);

type NamedTuple = (i32, [usize; 0]);

struct ConstParamZeroDefault<const N: usize = 0> {
    field: i32,
    last: [usize; N],
}

struct ConstParamNoDefault<const N: usize> {
    field: i32,
    last: [usize; N],
}

struct ConstParamNonZeroDefault<const N: usize = 1> {
    field: i32,
    last: [usize; N],
}

struct TwoGenericParams<T, const N: usize> {
    field: i32,
    last: [T; N],
}

type A = ConstParamZeroDefault;
type B = ConstParamZeroDefault<0>;
type C = ConstParamNoDefault<0>;
type D = ConstParamNonZeroDefault<0>;

fn main() {}

#[cfg(test)]
mod tests {
    pub struct Friend {
        age: u8,
    }

    #[test]
    fn oldest_empty_is_none() {
        struct Michael {
            friends: [Friend; 0],
        }
    }
}