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