]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/trailing_empty_array.rs
Rollup merge of #91055 - lcnr:type_of-closures, r=nikomatsakis
[rust.git] / src / tools / clippy / tests / ui / trailing_empty_array.rs
1 #![warn(clippy::trailing_empty_array)]
2
3 // Do lint:
4
5 struct RarelyUseful {
6     field: i32,
7     last: [usize; 0],
8 }
9
10 struct OnlyField {
11     first_and_last: [usize; 0],
12 }
13
14 struct GenericArrayType<T> {
15     field: i32,
16     last: [T; 0],
17 }
18
19 #[must_use]
20 struct OnlyAnotherAttribute {
21     field: i32,
22     last: [usize; 0],
23 }
24
25 #[derive(Debug)]
26 struct OnlyADeriveAttribute {
27     field: i32,
28     last: [usize; 0],
29 }
30
31 const ZERO: usize = 0;
32 struct ZeroSizedWithConst {
33     field: i32,
34     last: [usize; ZERO],
35 }
36
37 #[allow(clippy::eq_op)]
38 const fn compute_zero() -> usize {
39     (4 + 6) - (2 * 5)
40 }
41 struct ZeroSizedWithConstFunction {
42     field: i32,
43     last: [usize; compute_zero()],
44 }
45
46 const fn compute_zero_from_arg(x: usize) -> usize {
47     x - 1
48 }
49 struct ZeroSizedWithConstFunction2 {
50     field: i32,
51     last: [usize; compute_zero_from_arg(1)],
52 }
53
54 struct ZeroSizedArrayWrapper([usize; 0]);
55
56 struct TupleStruct(i32, [usize; 0]);
57
58 struct LotsOfFields {
59     f1: u32,
60     f2: u32,
61     f3: u32,
62     f4: u32,
63     f5: u32,
64     f6: u32,
65     f7: u32,
66     f8: u32,
67     f9: u32,
68     f10: u32,
69     f11: u32,
70     f12: u32,
71     f13: u32,
72     f14: u32,
73     f15: u32,
74     f16: u32,
75     last: [usize; 0],
76 }
77
78 // Don't lint
79
80 #[repr(C)]
81 struct GoodReason {
82     field: i32,
83     last: [usize; 0],
84 }
85
86 #[repr(C)]
87 struct OnlyFieldWithReprC {
88     first_and_last: [usize; 0],
89 }
90
91 struct NonZeroSizedArray {
92     field: i32,
93     last: [usize; 1],
94 }
95
96 struct NotLastField {
97     f1: u32,
98     zero_sized: [usize; 0],
99     last: i32,
100 }
101
102 const ONE: usize = 1;
103 struct NonZeroSizedWithConst {
104     field: i32,
105     last: [usize; ONE],
106 }
107
108 #[derive(Debug)]
109 #[repr(C)]
110 struct AlsoADeriveAttribute {
111     field: i32,
112     last: [usize; 0],
113 }
114
115 #[must_use]
116 #[repr(C)]
117 struct AlsoAnotherAttribute {
118     field: i32,
119     last: [usize; 0],
120 }
121
122 #[repr(packed)]
123 struct ReprPacked {
124     field: i32,
125     last: [usize; 0],
126 }
127
128 #[repr(C, packed)]
129 struct ReprCPacked {
130     field: i32,
131     last: [usize; 0],
132 }
133
134 #[repr(align(64))]
135 struct ReprAlign {
136     field: i32,
137     last: [usize; 0],
138 }
139 #[repr(C, align(64))]
140 struct ReprCAlign {
141     field: i32,
142     last: [usize; 0],
143 }
144
145 // 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
146 #[repr(C)]
147 enum DontLintAnonymousStructsFromDesuraging {
148     A(u32),
149     B(f32, [u64; 0]),
150     C { x: u32, y: [u64; 0] },
151 }
152
153 #[repr(C)]
154 struct TupleStructReprC(i32, [usize; 0]);
155
156 type NamedTuple = (i32, [usize; 0]);
157
158 #[rustfmt::skip] // [rustfmt#4995](https://github.com/rust-lang/rustfmt/issues/4995)
159 struct ConstParamZeroDefault<const N: usize = 0> {
160     field: i32,
161     last: [usize; N],
162 }
163
164 struct ConstParamNoDefault<const N: usize> {
165     field: i32,
166     last: [usize; N],
167 }
168
169 #[rustfmt::skip] 
170 struct ConstParamNonZeroDefault<const N: usize = 1> {
171     field: i32,
172     last: [usize; N],
173 }
174
175 struct TwoGenericParams<T, const N: usize> {
176     field: i32,
177     last: [T; N],
178 }
179
180 type A = ConstParamZeroDefault;
181 type B = ConstParamZeroDefault<0>;
182 type C = ConstParamNoDefault<0>;
183 type D = ConstParamNonZeroDefault<0>;
184
185 fn main() {}