Skip to content

Commit cfddcf1

Browse files
[derive] Suppress non_camel_case_types... (#2057) (#2060)
...in derive-generated code. Closes #2051 gherrit-pr-id: If3dbedcccd338435b5aa72dd724caaf9447b1baa Co-authored-by: Joshua Liebow-Feeser <joshlf@users.noreply.github.com>
1 parent 90c438b commit cfddcf1

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

zerocopy-derive/src/enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn generate_tag_enum(repr: &EnumRepr, data: &DataEnum) -> TokenStream
3535

3636
quote! {
3737
#repr
38-
#[allow(dead_code)]
38+
#[allow(dead_code, non_camel_case_types)]
3939
enum ___ZerocopyTag {
4040
#(#variants,)*
4141
}

zerocopy-derive/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,9 @@ fn impl_block<D: DataExt>(
13511351
// TODO(#553): Add a test that generates a warning when
13521352
// `#[allow(deprecated)]` isn't present.
13531353
#[allow(deprecated)]
1354+
// While there are not currently any warnings that this suppresses (that
1355+
// we're aware of), it's good future-proofing hygiene.
1356+
#[automatically_derived]
13541357
unsafe impl < #(#params),* > #trait_path for #type_ident < #(#param_idents),* >
13551358
where
13561359
#(#bounds,)*

zerocopy-derive/src/output_tests.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ fn test_known_layout() {
102102
struct Foo;
103103
} expands to {
104104
#[allow(deprecated)]
105+
#[automatically_derived]
105106
unsafe impl ::zerocopy::KnownLayout for Foo
106107
where
107108
Self: ::zerocopy::util::macro_util::core_reexport::marker::Sized,
@@ -134,6 +135,7 @@ fn test_immutable() {
134135
struct Foo;
135136
} expands to {
136137
#[allow(deprecated)]
138+
#[automatically_derived]
137139
unsafe impl ::zerocopy::Immutable for Foo {
138140
fn only_derive_is_allowed_to_implement_this_trait() {}
139141
}
@@ -148,6 +150,7 @@ fn test_try_from_bytes() {
148150
struct Foo;
149151
} expands to {
150152
#[allow(deprecated)]
153+
#[automatically_derived]
151154
unsafe impl ::zerocopy::TryFromBytes for Foo {
152155
fn only_derive_is_allowed_to_implement_this_trait() {}
153156

@@ -171,6 +174,7 @@ fn test_from_zeros() {
171174
struct Foo;
172175
} expands to {
173176
#[allow(deprecated)]
177+
#[automatically_derived]
174178
unsafe impl ::zerocopy::TryFromBytes for Foo {
175179
fn only_derive_is_allowed_to_implement_this_trait() {}
176180

@@ -185,6 +189,7 @@ fn test_from_zeros() {
185189
}
186190

187191
#[allow(deprecated)]
192+
#[automatically_derived]
188193
unsafe impl ::zerocopy::FromZeros for Foo {
189194
fn only_derive_is_allowed_to_implement_this_trait() {}
190195
}
@@ -199,6 +204,7 @@ fn test_from_bytes_struct() {
199204
struct Foo;
200205
} expands to {
201206
#[allow(deprecated)]
207+
#[automatically_derived]
202208
unsafe impl ::zerocopy::TryFromBytes for Foo {
203209
fn only_derive_is_allowed_to_implement_this_trait() {}
204210

@@ -222,11 +228,13 @@ fn test_from_bytes_struct() {
222228
}
223229

224230
#[allow(deprecated)]
231+
#[automatically_derived]
225232
unsafe impl ::zerocopy::FromZeros for Foo {
226233
fn only_derive_is_allowed_to_implement_this_trait() {}
227234
}
228235

229236
#[allow(deprecated)]
237+
#[automatically_derived]
230238
unsafe impl ::zerocopy::FromBytes for Foo {
231239
fn only_derive_is_allowed_to_implement_this_trait() {}
232240
}
@@ -243,6 +251,7 @@ fn test_from_bytes_union() {
243251
}
244252
} expands to {
245253
#[allow(deprecated)]
254+
#[automatically_derived]
246255
unsafe impl ::zerocopy::TryFromBytes for Foo
247256
where
248257
u8: ::zerocopy::TryFromBytes + ::zerocopy::Immutable,
@@ -269,6 +278,7 @@ fn test_from_bytes_union() {
269278
}
270279

271280
#[allow(deprecated)]
281+
#[automatically_derived]
272282
unsafe impl ::zerocopy::FromZeros for Foo
273283
where
274284
u8: ::zerocopy::FromZeros + ::zerocopy::Immutable,
@@ -277,6 +287,7 @@ fn test_from_bytes_union() {
277287
}
278288

279289
#[allow(deprecated)]
290+
#[automatically_derived]
280291
unsafe impl ::zerocopy::FromBytes for Foo
281292
where
282293
u8: ::zerocopy::FromBytes + ::zerocopy::Immutable,
@@ -295,6 +306,7 @@ fn test_into_bytes() {
295306
struct Foo;
296307
} expands to {
297308
#[allow(deprecated)]
309+
#[automatically_derived]
298310
unsafe impl ::zerocopy::IntoBytes for Foo {
299311
fn only_derive_is_allowed_to_implement_this_trait() {}
300312
}
@@ -310,6 +322,7 @@ fn test_into_bytes() {
310322
}
311323
} expands to {
312324
#[allow(deprecated)]
325+
#[automatically_derived]
313326
unsafe impl ::zerocopy::IntoBytes for Foo
314327
where
315328
u8: ::zerocopy::IntoBytes,
@@ -333,6 +346,7 @@ fn test_unaligned() {
333346
struct Foo;
334347
} expands to {
335348
#[allow(deprecated)]
349+
#[automatically_derived]
336350
unsafe impl ::zerocopy::Unaligned for Foo {
337351
fn only_derive_is_allowed_to_implement_this_trait() {}
338352
}
@@ -355,6 +369,7 @@ fn test_try_from_bytes_enum() {
355369
}
356370
} expands to {
357371
#[allow(deprecated)]
372+
#[automatically_derived]
358373
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
359374
for ComplexWithGenerics<'a, { N }, X, Y>
360375
where
@@ -377,7 +392,7 @@ fn test_try_from_bytes_enum() {
377392
{
378393
use ::zerocopy::util::macro_util::core_reexport;
379394
#[repr(u8)]
380-
#[allow(dead_code)]
395+
#[allow(dead_code, non_camel_case_types)]
381396
enum ___ZerocopyTag {
382397
UnitLike,
383398
StructLike,
@@ -411,6 +426,7 @@ fn test_try_from_bytes_enum() {
411426
where
412427
X: Deref<Target = &'a [(X, Y); N]>;
413428
#[allow(deprecated)]
429+
#[automatically_derived]
414430
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
415431
for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
416432
where
@@ -506,6 +522,7 @@ fn test_try_from_bytes_enum() {
506522
where
507523
X: Deref<Target = &'a [(X, Y); N]>;
508524
#[allow(deprecated)]
525+
#[automatically_derived]
509526
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
510527
for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
511528
where
@@ -642,6 +659,7 @@ fn test_try_from_bytes_enum() {
642659
}
643660
} expands to {
644661
#[allow(deprecated)]
662+
#[automatically_derived]
645663
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
646664
for ComplexWithGenerics<'a, { N }, X, Y>
647665
where
@@ -664,7 +682,7 @@ fn test_try_from_bytes_enum() {
664682
{
665683
use ::zerocopy::util::macro_util::core_reexport;
666684
#[repr(u32)]
667-
#[allow(dead_code)]
685+
#[allow(dead_code, non_camel_case_types)]
668686
enum ___ZerocopyTag {
669687
UnitLike,
670688
StructLike,
@@ -698,6 +716,7 @@ fn test_try_from_bytes_enum() {
698716
where
699717
X: Deref<Target = &'a [(X, Y); N]>;
700718
#[allow(deprecated)]
719+
#[automatically_derived]
701720
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
702721
for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
703722
where
@@ -793,6 +812,7 @@ fn test_try_from_bytes_enum() {
793812
where
794813
X: Deref<Target = &'a [(X, Y); N]>;
795814
#[allow(deprecated)]
815+
#[automatically_derived]
796816
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
797817
for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
798818
where
@@ -929,6 +949,7 @@ fn test_try_from_bytes_enum() {
929949
}
930950
} expands to {
931951
#[allow(deprecated)]
952+
#[automatically_derived]
932953
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
933954
for ComplexWithGenerics<'a, { N }, X, Y>
934955
where
@@ -951,7 +972,7 @@ fn test_try_from_bytes_enum() {
951972
{
952973
use ::zerocopy::util::macro_util::core_reexport;
953974
#[repr(C)]
954-
#[allow(dead_code)]
975+
#[allow(dead_code, non_camel_case_types)]
955976
enum ___ZerocopyTag {
956977
UnitLike,
957978
StructLike,
@@ -985,6 +1006,7 @@ fn test_try_from_bytes_enum() {
9851006
where
9861007
X: Deref<Target = &'a [(X, Y); N]>;
9871008
#[allow(deprecated)]
1009+
#[automatically_derived]
9881010
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
9891011
for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
9901012
where
@@ -1080,6 +1102,7 @@ fn test_try_from_bytes_enum() {
10801102
where
10811103
X: Deref<Target = &'a [(X, Y); N]>;
10821104
#[allow(deprecated)]
1105+
#[automatically_derived]
10831106
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
10841107
for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
10851108
where
@@ -1471,6 +1494,7 @@ fn test_from_bytes_enum() {
14711494
}
14721495
} expands to {
14731496
#[allow(deprecated)]
1497+
#[automatically_derived]
14741498
unsafe impl ::zerocopy::TryFromBytes for Foo {
14751499
fn only_derive_is_allowed_to_implement_this_trait() {}
14761500

@@ -1494,11 +1518,13 @@ fn test_from_bytes_enum() {
14941518
}
14951519

14961520
#[allow(deprecated)]
1521+
#[automatically_derived]
14971522
unsafe impl ::zerocopy::FromZeros for Foo {
14981523
fn only_derive_is_allowed_to_implement_this_trait() {}
14991524
}
15001525

15011526
#[allow(deprecated)]
1527+
#[automatically_derived]
15021528
unsafe impl ::zerocopy::FromBytes for Foo {
15031529
fn only_derive_is_allowed_to_implement_this_trait() {}
15041530
}
@@ -1775,6 +1801,7 @@ fn test_try_from_bytes_trivial_is_bit_valid_enum() {
17751801
}
17761802
} expands to {
17771803
#[allow(deprecated)]
1804+
#[automatically_derived]
17781805
unsafe impl ::zerocopy::TryFromBytes for Foo {
17791806
fn only_derive_is_allowed_to_implement_this_trait() {}
17801807

zerocopy-derive/tests/enum_try_from_bytes.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,19 @@ fn test_trivial_is_bit_valid() {
626626
util_assert_not_impl_any!(FooU8: imp::FromBytes);
627627
util::test_trivial_is_bit_valid::<FooU8>();
628628
}
629+
630+
#[deny(non_camel_case_types)]
631+
mod issue_2051 {
632+
use super::*;
633+
634+
// Test that the `non_camel_case_types` lint isn't triggered by generated code.
635+
// Prevents regressions of #2051.
636+
#[repr(u32)]
637+
#[derive(imp::TryFromBytes)]
638+
#[allow(non_camel_case_types)]
639+
pub enum Code {
640+
I32_ADD,
641+
I32_SUB,
642+
I32_MUL,
643+
}
644+
}

0 commit comments

Comments
 (0)