Skip to content

Commit

Permalink
[derive] Suppress non_camel_case_types... (#2057)
Browse files Browse the repository at this point in the history
...in derive-generated code.

Closes #2051

gherrit-pr-id: If3dbedcccd338435b5aa72dd724caaf9447b1baa
  • Loading branch information
joshlf authored Nov 13, 2024
1 parent ad47fa2 commit b89e4b7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion zerocopy-derive/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn generate_tag_enum(repr: &EnumRepr, data: &DataEnum) -> TokenStream

quote! {
#repr
#[allow(dead_code)]
#[allow(dead_code, non_camel_case_types)]
enum ___ZerocopyTag {
#(#variants,)*
}
Expand Down
3 changes: 3 additions & 0 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,9 @@ fn impl_block<D: DataExt>(
// TODO(#553): Add a test that generates a warning when
// `#[allow(deprecated)]` isn't present.
#[allow(deprecated)]
// While there are not currently any warnings that this suppresses (that
// we're aware of), it's good future-proofing hygiene.
#[automatically_derived]
unsafe impl < #(#params),* > #trait_path for #type_ident < #(#param_idents),* >
where
#(#bounds,)*
Expand Down
33 changes: 30 additions & 3 deletions zerocopy-derive/src/output_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fn test_known_layout() {
struct Foo;
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::KnownLayout for Foo
where
Self: ::zerocopy::util::macro_util::core_reexport::marker::Sized,
Expand Down Expand Up @@ -134,6 +135,7 @@ fn test_immutable() {
struct Foo;
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::Immutable for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}
}
Expand All @@ -148,6 +150,7 @@ fn test_try_from_bytes() {
struct Foo;
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::TryFromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}

Expand All @@ -172,6 +175,7 @@ fn test_from_zeros() {
struct Foo;
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::TryFromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}

Expand All @@ -187,6 +191,7 @@ fn test_from_zeros() {
}

#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::FromZeros for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}
}
Expand All @@ -201,6 +206,7 @@ fn test_from_bytes_struct() {
struct Foo;
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::TryFromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}

Expand All @@ -225,11 +231,13 @@ fn test_from_bytes_struct() {
}

#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::FromZeros for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}
}

#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::FromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}
}
Expand All @@ -246,6 +254,7 @@ fn test_from_bytes_union() {
}
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::TryFromBytes for Foo
where
u8: ::zerocopy::TryFromBytes + ::zerocopy::Immutable,
Expand Down Expand Up @@ -273,6 +282,7 @@ fn test_from_bytes_union() {
}

#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::FromZeros for Foo
where
u8: ::zerocopy::FromZeros + ::zerocopy::Immutable,
Expand All @@ -281,6 +291,7 @@ fn test_from_bytes_union() {
}

#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::FromBytes for Foo
where
u8: ::zerocopy::FromBytes + ::zerocopy::Immutable,
Expand All @@ -299,6 +310,7 @@ fn test_into_bytes() {
struct Foo;
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::IntoBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}
}
Expand All @@ -314,6 +326,7 @@ fn test_into_bytes() {
}
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::IntoBytes for Foo
where
u8: ::zerocopy::IntoBytes,
Expand All @@ -337,6 +350,7 @@ fn test_unaligned() {
struct Foo;
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::Unaligned for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}
}
Expand All @@ -359,6 +373,7 @@ fn test_try_from_bytes_enum() {
}
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ComplexWithGenerics<'a, { N }, X, Y>
where
Expand All @@ -382,7 +397,7 @@ fn test_try_from_bytes_enum() {
{
use ::zerocopy::util::macro_util::core_reexport;
#[repr(u8)]
#[allow(dead_code)]
#[allow(dead_code, non_camel_case_types)]
enum ___ZerocopyTag {
UnitLike,
StructLike,
Expand Down Expand Up @@ -416,6 +431,7 @@ fn test_try_from_bytes_enum() {
where
X: Deref<Target = &'a [(X, Y); N]>;
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
where
Expand Down Expand Up @@ -512,6 +528,7 @@ fn test_try_from_bytes_enum() {
where
X: Deref<Target = &'a [(X, Y); N]>;
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
where
Expand Down Expand Up @@ -653,6 +670,7 @@ fn test_try_from_bytes_enum() {
}
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ComplexWithGenerics<'a, { N }, X, Y>
where
Expand All @@ -676,7 +694,7 @@ fn test_try_from_bytes_enum() {
{
use ::zerocopy::util::macro_util::core_reexport;
#[repr(u32)]
#[allow(dead_code)]
#[allow(dead_code, non_camel_case_types)]
enum ___ZerocopyTag {
UnitLike,
StructLike,
Expand Down Expand Up @@ -710,6 +728,7 @@ fn test_try_from_bytes_enum() {
where
X: Deref<Target = &'a [(X, Y); N]>;
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
where
Expand Down Expand Up @@ -806,6 +825,7 @@ fn test_try_from_bytes_enum() {
where
X: Deref<Target = &'a [(X, Y); N]>;
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
where
Expand Down Expand Up @@ -947,6 +967,7 @@ fn test_try_from_bytes_enum() {
}
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ComplexWithGenerics<'a, { N }, X, Y>
where
Expand All @@ -970,7 +991,7 @@ fn test_try_from_bytes_enum() {
{
use ::zerocopy::util::macro_util::core_reexport;
#[repr(C)]
#[allow(dead_code)]
#[allow(dead_code, non_camel_case_types)]
enum ___ZerocopyTag {
UnitLike,
StructLike,
Expand Down Expand Up @@ -1004,6 +1025,7 @@ fn test_try_from_bytes_enum() {
where
X: Deref<Target = &'a [(X, Y); N]>;
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ___ZerocopyVariantStruct_StructLike<'a, { N }, X, Y>
where
Expand Down Expand Up @@ -1100,6 +1122,7 @@ fn test_try_from_bytes_enum() {
where
X: Deref<Target = &'a [(X, Y); N]>;
#[allow(deprecated)]
#[automatically_derived]
unsafe impl<'a: 'static, const N: usize, X, Y: Deref> ::zerocopy::TryFromBytes
for ___ZerocopyVariantStruct_TupleLike<'a, { N }, X, Y>
where
Expand Down Expand Up @@ -1496,6 +1519,7 @@ fn test_from_bytes_enum() {
}
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::TryFromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}

Expand All @@ -1520,11 +1544,13 @@ fn test_from_bytes_enum() {
}

#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::FromZeros for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}
}

#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::FromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}
}
Expand Down Expand Up @@ -1801,6 +1827,7 @@ fn test_try_from_bytes_trivial_is_bit_valid_enum() {
}
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::TryFromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}

Expand Down
16 changes: 16 additions & 0 deletions zerocopy-derive/tests/enum_try_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,19 @@ fn test_trivial_is_bit_valid() {
util_assert_not_impl_any!(FooU8: imp::FromBytes);
util::test_trivial_is_bit_valid::<FooU8>();
}

#[deny(non_camel_case_types)]
mod issue_2051 {
use super::*;

// Test that the `non_camel_case_types` lint isn't triggered by generated code.
// Prevents regressions of #2051.
#[repr(u32)]
#[derive(imp::TryFromBytes)]
#[allow(non_camel_case_types)]
pub enum Code {
I32_ADD,
I32_SUB,
I32_MUL,
}
}

0 comments on commit b89e4b7

Please sign in to comment.