Skip to content

Commit

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

Closes #2051

gherrit-pr-id: If3dbedcccd338435b5aa72dd724caaf9447b1baa

Co-authored-by: Joshua Liebow-Feeser <[email protected]>
  • Loading branch information
google-pr-creation-bot and joshlf authored Nov 14, 2024
1 parent 90c438b commit cfddcf1
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 @@ -1351,6 +1351,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 @@ -171,6 +174,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 @@ -185,6 +189,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 @@ -199,6 +204,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 @@ -222,11 +228,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 @@ -243,6 +251,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 All @@ -269,6 +278,7 @@ fn test_from_bytes_union() {
}

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

#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::FromBytes for Foo
where
u8: ::zerocopy::FromBytes + ::zerocopy::Immutable,
Expand All @@ -295,6 +306,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 @@ -310,6 +322,7 @@ fn test_into_bytes() {
}
} expands to {
#[allow(deprecated)]
#[automatically_derived]
unsafe impl ::zerocopy::IntoBytes for Foo
where
u8: ::zerocopy::IntoBytes,
Expand All @@ -333,6 +346,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 @@ -355,6 +369,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 @@ -377,7 +392,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 @@ -411,6 +426,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 @@ -506,6 +522,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 @@ -642,6 +659,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 @@ -664,7 +682,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 @@ -698,6 +716,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 @@ -793,6 +812,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 @@ -929,6 +949,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 @@ -951,7 +972,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 @@ -985,6 +1006,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 @@ -1080,6 +1102,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 @@ -1471,6 +1494,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 @@ -1494,11 +1518,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 @@ -1775,6 +1801,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 cfddcf1

Please sign in to comment.