Fix the errors CI is showing (#247)

* Change #![allow(clippy::missing_docs_in_private_items)] to #[allow(clippy::missing_docs_in_private_items)], also rustfmt went wild

* fix for 1.34 building.

* clippy be quiet
This commit is contained in:
Lokathor 2024-05-28 12:22:59 -06:00 committed by GitHub
parent a630404e18
commit c9540064f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 123 additions and 66 deletions

View File

@ -25,13 +25,17 @@ pub trait Derivable {
fn implies_trait(_crate_name: &TokenStream) -> Option<TokenStream> { fn implies_trait(_crate_name: &TokenStream) -> Option<TokenStream> {
None None
} }
fn asserts(_input: &DeriveInput, _crate_name: &TokenStream) -> Result<TokenStream> { fn asserts(
_input: &DeriveInput, _crate_name: &TokenStream,
) -> Result<TokenStream> {
Ok(quote!()) Ok(quote!())
} }
fn check_attributes(_ty: &Data, _attributes: &[Attribute]) -> Result<()> { fn check_attributes(_ty: &Data, _attributes: &[Attribute]) -> Result<()> {
Ok(()) Ok(())
} }
fn trait_impl(_input: &DeriveInput, _crate_name: &TokenStream) -> Result<(TokenStream, TokenStream)> { fn trait_impl(
_input: &DeriveInput, _crate_name: &TokenStream,
) -> Result<(TokenStream, TokenStream)> {
Ok((quote!(), quote!())) Ok((quote!(), quote!()))
} }
fn requires_where_clause() -> bool { fn requires_where_clause() -> bool {
@ -49,7 +53,9 @@ impl Derivable for Pod {
Ok(syn::parse_quote!(#crate_name::Pod)) Ok(syn::parse_quote!(#crate_name::Pod))
} }
fn asserts(input: &DeriveInput, crate_name: &TokenStream) -> Result<TokenStream> { fn asserts(
input: &DeriveInput, crate_name: &TokenStream,
) -> Result<TokenStream> {
let repr = get_repr(&input.attrs)?; let repr = get_repr(&input.attrs)?;
let completly_packed = let completly_packed =
@ -106,10 +112,14 @@ impl Derivable for AnyBitPattern {
Some(quote!(#crate_name::Zeroable)) Some(quote!(#crate_name::Zeroable))
} }
fn asserts(input: &DeriveInput, crate_name: &TokenStream) -> Result<TokenStream> { fn asserts(
input: &DeriveInput, crate_name: &TokenStream,
) -> Result<TokenStream> {
match &input.data { match &input.data {
Data::Union(_) => Ok(quote!()), // unions are always `AnyBitPattern` Data::Union(_) => Ok(quote!()), // unions are always `AnyBitPattern`
Data::Struct(_) => generate_fields_are_trait(input, Self::ident(input, crate_name)?), Data::Struct(_) => {
generate_fields_are_trait(input, Self::ident(input, crate_name)?)
}
Data::Enum(_) => { Data::Enum(_) => {
bail!("Deriving AnyBitPattern is not supported for enums") bail!("Deriving AnyBitPattern is not supported for enums")
} }
@ -128,7 +138,7 @@ impl Derivable for Zeroable {
let repr = get_repr(attributes)?; let repr = get_repr(attributes)?;
match ty { match ty {
Data::Struct(_) => Ok(()), Data::Struct(_) => Ok(()),
Data::Enum(DataEnum { variants,.. }) => { Data::Enum(DataEnum { variants, .. }) => {
if !repr.repr.is_integer() { if !repr.repr.is_integer() {
bail!("Zeroable requires the enum to be an explicit #[repr(Int)]") bail!("Zeroable requires the enum to be an explicit #[repr(Int)]")
} }
@ -151,15 +161,19 @@ impl Derivable for Zeroable {
} }
Ok(()) Ok(())
}, }
Data::Union(_) => Ok(()) Data::Union(_) => Ok(()),
} }
} }
fn asserts(input: &DeriveInput, crate_name: &TokenStream) -> Result<TokenStream> { fn asserts(
input: &DeriveInput, crate_name: &TokenStream,
) -> Result<TokenStream> {
match &input.data { match &input.data {
Data::Union(_) => Ok(quote!()), // unions are always `Zeroable` Data::Union(_) => Ok(quote!()), // unions are always `Zeroable`
Data::Struct(_) => generate_fields_are_trait(input, Self::ident(input, crate_name)?), Data::Struct(_) => {
generate_fields_are_trait(input, Self::ident(input, crate_name)?)
}
Data::Enum(_) => Ok(quote!()), Data::Enum(_) => Ok(quote!()),
} }
} }
@ -192,7 +206,9 @@ impl Derivable for NoUninit {
} }
} }
fn asserts(input: &DeriveInput, crate_name: &TokenStream) -> Result<TokenStream> { fn asserts(
input: &DeriveInput, crate_name: &TokenStream,
) -> Result<TokenStream> {
if !input.generics.params.is_empty() { if !input.generics.params.is_empty() {
bail!("NoUninit cannot be derived for structs containing generic parameters because the padding requirements can't be verified for generic structs"); bail!("NoUninit cannot be derived for structs containing generic parameters because the padding requirements can't be verified for generic structs");
} }
@ -219,7 +235,9 @@ impl Derivable for NoUninit {
} }
} }
fn trait_impl(_input: &DeriveInput, _crate_name: &TokenStream) -> Result<(TokenStream, TokenStream)> { fn trait_impl(
_input: &DeriveInput, _crate_name: &TokenStream,
) -> Result<(TokenStream, TokenStream)> {
Ok((quote!(), quote!())) Ok((quote!(), quote!()))
} }
} }
@ -255,7 +273,9 @@ impl Derivable for CheckedBitPattern {
} }
} }
fn asserts(input: &DeriveInput, crate_name: &TokenStream) -> Result<TokenStream> { fn asserts(
input: &DeriveInput, crate_name: &TokenStream,
) -> Result<TokenStream> {
if !input.generics.params.is_empty() { if !input.generics.params.is_empty() {
bail!("CheckedBitPattern cannot be derived for structs containing generic parameters"); bail!("CheckedBitPattern cannot be derived for structs containing generic parameters");
} }
@ -267,15 +287,23 @@ impl Derivable for CheckedBitPattern {
Ok(assert_fields_are_maybe_pod) Ok(assert_fields_are_maybe_pod)
} }
Data::Enum(_) => Ok(quote!()), /* nothing needed, already guaranteed OK by NoUninit */ Data::Enum(_) => Ok(quote!()), /* nothing needed, already guaranteed
* OK by NoUninit */
Data::Union(_) => bail!("Internal error in CheckedBitPattern derive"), /* shouldn't be possible since we already error in attribute check for this case */ Data::Union(_) => bail!("Internal error in CheckedBitPattern derive"), /* shouldn't be possible since we already error in attribute check for this case */
} }
} }
fn trait_impl(input: &DeriveInput, crate_name: &TokenStream) -> Result<(TokenStream, TokenStream)> { fn trait_impl(
input: &DeriveInput, crate_name: &TokenStream,
) -> Result<(TokenStream, TokenStream)> {
match &input.data { match &input.data {
Data::Struct(DataStruct { fields, .. }) => { Data::Struct(DataStruct { fields, .. }) => {
generate_checked_bit_pattern_struct(&input.ident, fields, &input.attrs, crate_name) generate_checked_bit_pattern_struct(
&input.ident,
fields,
&input.attrs,
crate_name,
)
} }
Data::Enum(DataEnum { variants, .. }) => { Data::Enum(DataEnum { variants, .. }) => {
generate_checked_bit_pattern_enum(input, variants, crate_name) generate_checked_bit_pattern_enum(input, variants, crate_name)
@ -322,7 +350,9 @@ impl Derivable for TransparentWrapper {
Ok(syn::parse_quote!(#crate_name::TransparentWrapper<#ty>)) Ok(syn::parse_quote!(#crate_name::TransparentWrapper<#ty>))
} }
fn asserts(input: &DeriveInput, crate_name: &TokenStream) -> Result<TokenStream> { fn asserts(
input: &DeriveInput, crate_name: &TokenStream,
) -> Result<TokenStream> {
let (impl_generics, _ty_generics, where_clause) = let (impl_generics, _ty_generics, where_clause) =
input.generics.split_for_impl(); input.generics.split_for_impl();
let fields = get_struct_fields(input)?; let fields = get_struct_fields(input)?;
@ -390,7 +420,9 @@ impl Derivable for Contiguous {
Ok(syn::parse_quote!(#crate_name::Contiguous)) Ok(syn::parse_quote!(#crate_name::Contiguous))
} }
fn trait_impl(input: &DeriveInput, _crate_name: &TokenStream) -> Result<(TokenStream, TokenStream)> { fn trait_impl(
input: &DeriveInput, _crate_name: &TokenStream,
) -> Result<(TokenStream, TokenStream)> {
let repr = get_repr(&input.attrs)?; let repr = get_repr(&input.attrs)?;
let integer_ty = if let Some(integer_ty) = repr.repr.as_integer() { let integer_ty = if let Some(integer_ty) = repr.repr.as_integer() {
@ -431,19 +463,19 @@ impl Derivable for Contiguous {
let min_lit = LitInt::new(&format!("{}", min), input.span()); let min_lit = LitInt::new(&format!("{}", min), input.span());
let max_lit = LitInt::new(&format!("{}", max), input.span()); let max_lit = LitInt::new(&format!("{}", max), input.span());
// `from_integer` and `into_integer` are usually provided by the trait's default implementation. // `from_integer` and `into_integer` are usually provided by the trait's
// We override this implementation because it goes through `transmute_copy`, which can lead to // default implementation. We override this implementation because it
// inefficient assembly as seen in https://github.com/Lokathor/bytemuck/issues/175 . // goes through `transmute_copy`, which can lead to inefficient assembly as seen in https://github.com/Lokathor/bytemuck/issues/175 .
Ok(( Ok((
quote!(), quote!(),
quote! { quote! {
type Int = #integer_ty; type Int = #integer_ty;
#![allow(clippy::missing_docs_in_private_items)] #[allow(clippy::missing_docs_in_private_items)]
const MIN_VALUE: #integer_ty = #min_lit; const MIN_VALUE: #integer_ty = #min_lit;
#![allow(clippy::missing_docs_in_private_items)] #[allow(clippy::missing_docs_in_private_items)]
const MAX_VALUE: #integer_ty = #max_lit; const MAX_VALUE: #integer_ty = #max_lit;
#[inline] #[inline]
@ -498,7 +530,8 @@ fn get_field_types<'a>(
} }
fn generate_checked_bit_pattern_struct( fn generate_checked_bit_pattern_struct(
input_ident: &Ident, fields: &Fields, attrs: &[Attribute], crate_name: &TokenStream input_ident: &Ident, fields: &Fields, attrs: &[Attribute],
crate_name: &TokenStream,
) -> Result<(TokenStream, TokenStream)> { ) -> Result<(TokenStream, TokenStream)> {
let bits_ty = Ident::new(&format!("{}Bits", input_ident), input_ident.span()); let bits_ty = Ident::new(&format!("{}Bits", input_ident), input_ident.span());
@ -545,7 +578,8 @@ fn generate_checked_bit_pattern_struct(
} }
fn generate_checked_bit_pattern_enum( fn generate_checked_bit_pattern_enum(
input: &DeriveInput, variants: &Punctuated<Variant, Token![,]>, crate_name: &TokenStream input: &DeriveInput, variants: &Punctuated<Variant, Token![,]>,
crate_name: &TokenStream,
) -> Result<(TokenStream, TokenStream)> { ) -> Result<(TokenStream, TokenStream)> {
if enum_has_fields(variants.iter()) { if enum_has_fields(variants.iter()) {
generate_checked_bit_pattern_enum_with_fields(input, variants, crate_name) generate_checked_bit_pattern_enum_with_fields(input, variants, crate_name)
@ -614,7 +648,8 @@ fn generate_checked_bit_pattern_enum_without_fields(
} }
fn generate_checked_bit_pattern_enum_with_fields( fn generate_checked_bit_pattern_enum_with_fields(
input: &DeriveInput, variants: &Punctuated<Variant, Token![,]>, crate_name: &TokenStream input: &DeriveInput, variants: &Punctuated<Variant, Token![,]>,
crate_name: &TokenStream,
) -> Result<(TokenStream, TokenStream)> { ) -> Result<(TokenStream, TokenStream)> {
let representation = get_repr(&input.attrs)?; let representation = get_repr(&input.attrs)?;
let vis = &input.vis; let vis = &input.vis;
@ -634,23 +669,28 @@ fn generate_checked_bit_pattern_enum_with_fields(
let bits_repr = Representation { repr: Repr::C, ..representation }; let bits_repr = Representation { repr: Repr::C, ..representation };
// the enum manually re-configured as the actual tagged union it represents, // the enum manually re-configured as the actual tagged union it
// thus circumventing the requirements rust imposes on the tag even when using // represents, thus circumventing the requirements rust imposes on
// #[repr(C)] enum layout // the tag even when using #[repr(C)] enum layout
// see: https://doc.rust-lang.org/reference/type-layout.html#reprc-enums-with-fields // see: https://doc.rust-lang.org/reference/type-layout.html#reprc-enums-with-fields
let bits_ty_ident = Ident::new(&format!("{input_ident}Bits"), input.span()); let bits_ty_ident =
Ident::new(&format!("{input_ident}Bits"), input.span());
// the variants union part of the tagged union. These get put into a union which gets the // the variants union part of the tagged union. These get put into a union
// AnyBitPattern derive applied to it, thus checking that the fields of the union obey the requriements of AnyBitPattern. // which gets the AnyBitPattern derive applied to it, thus checking
// The types that actually go in the union are one more level of indirection deep: we generate new structs for each variant // that the fields of the union obey the requriements of AnyBitPattern.
// (`variant_struct_definitions`) which themselves have the `CheckedBitPattern` derive applied, thus generating `{variant_struct_ident}Bits` // The types that actually go in the union are one more level of
// structs, which are the ones that go into this union. // indirection deep: we generate new structs for each variant
// (`variant_struct_definitions`) which themselves have the
// `CheckedBitPattern` derive applied, thus generating
// `{variant_struct_ident}Bits` structs, which are the ones that go
// into this union.
let variants_union_ident = let variants_union_ident =
Ident::new(&format!("{}Variants", input.ident), input.span()); Ident::new(&format!("{}Variants", input.ident), input.span());
let variant_struct_idents = variants let variant_struct_idents = variants.iter().map(|v| {
.iter() Ident::new(&format!("{input_ident}Variant{}", v.ident), v.span())
.map(|v| Ident::new(&format!("{input_ident}Variant{}", v.ident), v.span())); });
let variant_struct_definitions = let variant_struct_definitions =
variant_struct_idents.clone().zip(variants.iter()).map(|(variant_struct_ident, v)| { variant_struct_idents.clone().zip(variants.iter()).map(|(variant_struct_ident, v)| {
@ -663,8 +703,10 @@ fn generate_checked_bit_pattern_enum_with_fields(
} }
}); });
let union_fields = let union_fields = variant_struct_idents
variant_struct_idents.clone().zip(variants.iter()).map(|(variant_struct_ident, v)| { .clone()
.zip(variants.iter())
.map(|(variant_struct_ident, v)| {
let variant_struct_bits_ident = let variant_struct_bits_ident =
Ident::new(&format!("{variant_struct_ident}Bits"), input.span()); Ident::new(&format!("{variant_struct_ident}Bits"), input.span());
let field_ident = &v.ident; let field_ident = &v.ident;
@ -764,17 +806,20 @@ fn generate_checked_bit_pattern_enum_with_fields(
let bits_repr = Representation { repr: Repr::C, ..representation }; let bits_repr = Representation { repr: Repr::C, ..representation };
let input_ident = &input.ident; let input_ident = &input.ident;
// the enum manually re-configured as the union it represents. such a union is the union of variants // the enum manually re-configured as the union it represents. such a
// as a repr(c) struct with the discriminator type inserted at the beginning. // union is the union of variants as a repr(c) struct with the
// in our case we union the `Bits` representation of each variant rather than the variant itself, which we generate // discriminator type inserted at the beginning. in our case we
// via a nested `CheckedBitPattern` derive on the `variant_struct_definitions` generated below. // union the `Bits` representation of each variant rather than the variant
// itself, which we generate via a nested `CheckedBitPattern` derive
// on the `variant_struct_definitions` generated below.
// //
// see: https://doc.rust-lang.org/reference/type-layout.html#primitive-representation-of-enums-with-fields // see: https://doc.rust-lang.org/reference/type-layout.html#primitive-representation-of-enums-with-fields
let bits_ty_ident = Ident::new(&format!("{input_ident}Bits"), input.span()); let bits_ty_ident =
Ident::new(&format!("{input_ident}Bits"), input.span());
let variant_struct_idents = variants let variant_struct_idents = variants.iter().map(|v| {
.iter() Ident::new(&format!("{input_ident}Variant{}", v.ident), v.span())
.map(|v| Ident::new(&format!("{input_ident}Variant{}", v.ident), v.span())); });
let variant_struct_definitions = let variant_struct_definitions =
variant_struct_idents.clone().zip(variants.iter()).map(|(variant_struct_ident, v)| { variant_struct_idents.clone().zip(variants.iter()).map(|(variant_struct_ident, v)| {
@ -788,8 +833,10 @@ fn generate_checked_bit_pattern_enum_with_fields(
} }
}); });
let union_fields = let union_fields = variant_struct_idents
variant_struct_idents.clone().zip(variants.iter()).map(|(variant_struct_ident, v)| { .clone()
.zip(variants.iter())
.map(|(variant_struct_ident, v)| {
let variant_struct_bits_ident = let variant_struct_bits_ident =
Ident::new(&format!("{variant_struct_ident}Bits"), input.span()); Ident::new(&format!("{variant_struct_ident}Bits"), input.span());
let field_ident = &v.ident; let field_ident = &v.ident;

View File

@ -1,4 +1,5 @@
#![cfg(feature = "extern_crate_alloc")] #![cfg(feature = "extern_crate_alloc")]
#![allow(clippy::duplicated_attributes)]
//! Stuff to boost things in the `alloc` crate. //! Stuff to boost things in the `alloc` crate.
//! //!

View File

@ -2,12 +2,12 @@ use crate::{Pod, Zeroable};
/// Marker trait for "plain old data" types that are valid for any bit pattern. /// Marker trait for "plain old data" types that are valid for any bit pattern.
/// ///
/// The requirements for this is very similar to [`Pod`], /// The requirements for this is very similar to [`Pod`], except that the type
/// except that the type can allow uninit (or padding) bytes. /// can allow uninit (or padding) bytes. This limits what you can do with a type
/// This limits what you can do with a type of this kind, but also broadens the /// of this kind, but also broadens the included types to `repr(C)` `struct`s
/// included types to `repr(C)` `struct`s that contain padding as well as /// that contain padding as well as `union`s. Notably, you can only cast
/// `union`s. Notably, you can only cast *immutable* references and *owned* /// *immutable* references and *owned* values into [`AnyBitPattern`] types, not
/// values into [`AnyBitPattern`] types, not *mutable* references. /// *mutable* references.
/// ///
/// [`Pod`] is a subset of [`AnyBitPattern`], meaning that any `T: Pod` is also /// [`Pod`] is a subset of [`AnyBitPattern`], meaning that any `T: Pod` is also
/// [`AnyBitPattern`] but any `T: AnyBitPattern` is not necessarily [`Pod`]. /// [`AnyBitPattern`] but any `T: AnyBitPattern` is not necessarily [`Pod`].
@ -26,8 +26,8 @@ use crate::{Pod, Zeroable};
/// below safety rules. /// below safety rules.
/// ///
/// * *NOTE: even `C-style`, fieldless enums are intentionally **excluded** from /// * *NOTE: even `C-style`, fieldless enums are intentionally **excluded** from
/// this trait, since it is **unsound** for an enum to have a discriminant value /// this trait, since it is **unsound** for an enum to have a discriminant
/// that is not one of its defined variants. /// value that is not one of its defined variants.
/// ///
/// # Safety /// # Safety
/// ///
@ -56,6 +56,9 @@ pub unsafe trait AnyBitPattern:
unsafe impl<T: Pod> AnyBitPattern for T {} unsafe impl<T: Pod> AnyBitPattern for T {}
#[cfg(feature = "zeroable_maybe_uninit")] #[cfg(feature = "zeroable_maybe_uninit")]
#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "zeroable_maybe_uninit")))] #[cfg_attr(
feature = "nightly_docs",
doc(cfg(feature = "zeroable_maybe_uninit"))
)]
unsafe impl<T> AnyBitPattern for core::mem::MaybeUninit<T> where T: AnyBitPattern unsafe impl<T> AnyBitPattern for core::mem::MaybeUninit<T> where T: AnyBitPattern
{} {}

View File

@ -114,13 +114,13 @@ use crate::{
/// ///
/// # Safety /// # Safety
/// ///
/// * `Self` *must* have the same layout as the specified `Bits` except for /// * `Self` *must* have the same layout as the specified `Bits` except for the
/// the possible invalid bit patterns being checked during /// possible invalid bit patterns being checked during
/// [`is_valid_bit_pattern`]. /// [`is_valid_bit_pattern`].
/// * This almost certainly means your type must be `#[repr(C)]` or a similar /// * This almost certainly means your type must be `#[repr(C)]` or a similar
/// specified repr, but if you think you know better, you probably don't. If /// specified repr, but if you think you know better, you probably don't. If
/// you still think you know better, be careful and have fun. And don't mess /// you still think you know better, be careful and have fun. And don't mess
/// it up (I mean it). /// it up (I mean it).
/// * If [`is_valid_bit_pattern`] returns true, then the bit pattern contained /// * If [`is_valid_bit_pattern`] returns true, then the bit pattern contained
/// in `bits` must also be valid for an instance of `Self`. /// in `bits` must also be valid for an instance of `Self`.
/// * Probably more, don't mess it up (I mean it 2.0) /// * Probably more, don't mess it up (I mean it 2.0)
@ -163,7 +163,11 @@ unsafe impl CheckedBitPattern for bool {
#[inline] #[inline]
fn is_valid_bit_pattern(bits: &Self::Bits) -> bool { fn is_valid_bit_pattern(bits: &Self::Bits) -> bool {
matches!(*bits, 0 | 1) // DO NOT use the `matches!` macro, it isn't 1.34 compatible.
match *bits {
0 | 1 => true,
_ => false,
}
} }
} }

View File

@ -2,6 +2,8 @@
#![warn(missing_docs)] #![warn(missing_docs)]
#![allow(clippy::match_like_matches_macro)] #![allow(clippy::match_like_matches_macro)]
#![allow(clippy::uninlined_format_args)] #![allow(clippy::uninlined_format_args)]
#![allow(clippy::result_unit_err)]
#![allow(clippy::type_complexity)]
#![cfg_attr(feature = "nightly_docs", feature(doc_cfg))] #![cfg_attr(feature = "nightly_docs", feature(doc_cfg))]
#![cfg_attr(feature = "nightly_portable_simd", feature(portable_simd))] #![cfg_attr(feature = "nightly_portable_simd", feature(portable_simd))]
#![cfg_attr( #![cfg_attr(