From 7974ce300431d88f0bfe8ebec9ec2a7d24c9e730 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Sun, 4 Aug 2024 22:54:17 +0200 Subject: [PATCH] Remove `allow(unused)` in favor of `cfg` guards --- src/data.rs | 17 +++++++++++++---- src/error.rs | 2 ++ src/input.rs | 6 +++++- src/item.rs | 22 ++++++++++++++++------ src/lib.rs | 4 +++- src/trait_/clone.rs | 2 +- src/trait_/common_ord.rs | 2 +- src/trait_/debug.rs | 2 +- src/trait_/default.rs | 2 +- src/trait_/hash.rs | 2 +- src/trait_/ord.rs | 2 +- src/trait_/partial_eq.rs | 2 +- src/trait_/partial_ord.rs | 2 +- src/trait_/zeroize.rs | 2 +- src/trait_/zeroize_on_drop.rs | 2 +- 15 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/data.rs b/src/data.rs index afe3008..e2f3553 100644 --- a/src/data.rs +++ b/src/data.rs @@ -4,7 +4,9 @@ mod field; mod fields; use proc_macro2::Span; -use syn::{Expr, FieldsNamed, Ident, Pat, PatPath, Path, Result, Variant}; +#[cfg(not(feature = "nightly"))] +use syn::Expr; +use syn::{FieldsNamed, Ident, Pat, PatPath, Path, Result, Variant}; pub use self::{ field::{Field, Member}, @@ -27,7 +29,7 @@ pub struct Data<'a> { pub path: Path, /// [Type](DataType) of this struct, union or variant. pub type_: DataType<'a>, - #[cfg_attr(feature = "nightly", allow(unused))] + #[cfg(not(feature = "nightly"))] /// Discriminant of this variant. pub discriminant: Option<&'a Expr>, } @@ -70,7 +72,7 @@ pub enum SimpleType<'a> { /// Tuple struct or tuple variant. Tuple(&'a Fields<'a>), /// Union. - Union(#[allow(unused)] &'a Fields<'a>), + Union, /// Unit variant. Unit(&'a Pat), } @@ -101,6 +103,7 @@ impl<'a> Data<'a> { ident, path, type_: DataType::Struct(fields), + #[cfg(not(feature = "nightly"))] discriminant: None, }) } @@ -118,6 +121,7 @@ impl<'a> Data<'a> { ident, path, type_: DataType::Tuple(fields), + #[cfg(not(feature = "nightly"))] discriminant: None, }) } @@ -132,6 +136,7 @@ impl<'a> Data<'a> { qself: None, path, })), + #[cfg(not(feature = "nightly"))] discriminant: None, }), syn::Fields::Unit => Err(Error::item_empty(span)), @@ -159,6 +164,7 @@ impl<'a> Data<'a> { ident, path, type_: DataType::Union(fields), + #[cfg(not(feature = "nightly"))] discriminant: None, }) } @@ -192,6 +198,7 @@ impl<'a> Data<'a> { default, type_: VariantType::Struct(fields), }, + #[cfg(not(feature = "nightly"))] discriminant: variant.discriminant.as_ref().map(|(_, expr)| expr), }) } @@ -208,6 +215,7 @@ impl<'a> Data<'a> { default, type_: VariantType::Tuple(fields), }, + #[cfg(not(feature = "nightly"))] discriminant: variant.discriminant.as_ref().map(|(_, expr)| expr), }) } @@ -227,6 +235,7 @@ impl<'a> Data<'a> { default, type_: VariantType::Unit(pattern), }, + #[cfg(not(feature = "nightly"))] discriminant: variant.discriminant.as_ref().map(|(_, expr)| expr), }) } @@ -328,7 +337,7 @@ impl<'a> Data<'a> { type_: VariantType::Unit(pattern), .. } => SimpleType::Unit(pattern), - DataType::Union(fields) => SimpleType::Union(fields), + DataType::Union(_) => SimpleType::Union, } } diff --git a/src/error.rs b/src/error.rs index a476e40..9557ac5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -213,12 +213,14 @@ impl Error { } /// Unknown `repr`. + #[cfg(not(feature = "nightly"))] pub fn repr_unknown(span: Span) -> syn::Error { syn::Error::new(span, "found unknown representation") } /// Invalid enum with non-empty variants and custom discriminants without an /// integer representation. + #[cfg(not(feature = "nightly"))] pub fn repr_discriminant_invalid(span: Span) -> syn::Error { syn::Error::new( span, diff --git a/src/input.rs b/src/input.rs index 040fc61..55a52fe 100644 --- a/src/input.rs +++ b/src/input.rs @@ -5,7 +5,9 @@ use syn::{DeriveInput, GenericParam, Generics, ImplGenerics, Result, TypeGeneric #[cfg(feature = "zeroize")] use crate::DeriveTrait; -use crate::{Data, DeriveWhere, Discriminant, Either, Error, Item, ItemAttr, Trait}; +#[cfg(not(feature = "nightly"))] +use crate::Discriminant; +use crate::{Data, DeriveWhere, Either, Error, Item, ItemAttr, Trait}; /// Parsed input. pub struct Input<'a> { @@ -51,6 +53,7 @@ impl<'a> Input<'a> { ) .map(Item::Item)?, syn::Data::Enum(data) => { + #[cfg(not(feature = "nightly"))] let discriminant = Discriminant::parse(attrs, &data.variants)?; let variants = data @@ -99,6 +102,7 @@ impl<'a> Input<'a> { } Item::Enum { + #[cfg(not(feature = "nightly"))] discriminant, ident, variants, diff --git a/src/item.rs b/src/item.rs index 2872f36..e94f82f 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,10 +1,16 @@ //! Intermediate representation of item data. -use proc_macro2::{Ident, Span, TokenStream, TokenTree}; -use quote::ToTokens; -use syn::{punctuated::Punctuated, spanned::Spanned, Attribute, Meta, Result, Token, Variant}; +use proc_macro2::Ident; +#[cfg(not(feature = "nightly"))] +use { + proc_macro2::{Span, TokenStream, TokenTree}, + quote::ToTokens, + syn::{punctuated::Punctuated, spanned::Spanned, Attribute, Meta, Result, Token, Variant}, +}; -use crate::{Data, Error, Incomparable, Trait}; +#[cfg(not(feature = "nightly"))] +use crate::Error; +use crate::{Data, Incomparable, Trait}; /// Fields or variants of an item. #[cfg_attr(test, derive(Debug))] @@ -12,7 +18,7 @@ use crate::{Data, Error, Incomparable, Trait}; pub enum Item<'a> { /// Enum. Enum { - #[cfg_attr(feature = "nightly", allow(unused))] + #[cfg(not(feature = "nightly"))] /// Type of discriminant used. discriminant: Discriminant, /// [`struct@Ident`] of this enum. @@ -97,7 +103,7 @@ impl Item<'_> { /// Type of discriminant used. #[derive(Clone, Copy)] #[cfg_attr(test, derive(Debug))] -#[cfg_attr(feature = "nightly", allow(unused))] +#[cfg(not(feature = "nightly"))] pub enum Discriminant { /// The enum has only a single variant. Single, @@ -111,6 +117,7 @@ pub enum Discriminant { DataRepr(Representation), } +#[cfg(not(feature = "nightly"))] impl Discriminant { /// Parse the representation of an item. pub fn parse(attrs: &[Attribute], variants: &Punctuated) -> Result { @@ -167,6 +174,7 @@ impl Discriminant { /// The type used to represent an enum. #[derive(Clone, Copy)] #[cfg_attr(test, derive(Debug))] +#[cfg(not(feature = "nightly"))] pub enum Representation { /// [`u8`]. U8, @@ -194,6 +202,7 @@ pub enum Representation { ISize, } +#[cfg(not(feature = "nightly"))] impl Representation { /// Parse an [`struct@Ident`] to a valid representation if it is. fn parse(ident: &Ident) -> Option { @@ -247,6 +256,7 @@ impl Representation { } } +#[cfg(not(feature = "nightly"))] impl ToTokens for Representation { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend(self.to_token()); diff --git a/src/lib.rs b/src/lib.rs index 89feddb..5db30e3 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -408,6 +408,8 @@ use util::MetaListExt; #[cfg(feature = "zeroize")] use self::attr::ZeroizeFqs; +#[cfg(not(feature = "nightly"))] +use self::item::Discriminant; use self::{ attr::{ Default, DeriveTrait, DeriveWhere, FieldAttr, Incomparable, ItemAttr, Skip, SkipGroup, @@ -416,7 +418,7 @@ use self::{ data::{Data, DataType, Field, SimpleType}, error::Error, input::Input, - item::{Discriminant, Item}, + item::Item, trait_::{Trait, TraitImpl}, util::Either, }; diff --git a/src/trait_/clone.rs b/src/trait_/clone.rs index 50ae6d1..dd08a33 100644 --- a/src/trait_/clone.rs +++ b/src/trait_/clone.rs @@ -123,7 +123,7 @@ impl TraitImpl for Clone { SimpleType::Unit(pattern) => { quote! { #pattern => #pattern, } } - SimpleType::Union(_) => TokenStream::new(), + SimpleType::Union => TokenStream::new(), } } } diff --git a/src/trait_/common_ord.rs b/src/trait_/common_ord.rs index 7f713a6..88ab11d 100644 --- a/src/trait_/common_ord.rs +++ b/src/trait_/common_ord.rs @@ -461,7 +461,7 @@ pub fn build_incomparable_pattern(variants: &[Data]) -> Option { .map(|variant @ Data { path, .. }| match variant.simple_type() { SimpleType::Struct(_) => quote!(#path{..}), SimpleType::Tuple(_) => quote!(#path(..)), - SimpleType::Union(_) => unreachable!("enum variants cannot be unions"), + SimpleType::Union => unreachable!("enum variants cannot be unions"), SimpleType::Unit(_) => quote!(#path), }) .peekable(); diff --git a/src/trait_/debug.rs b/src/trait_/debug.rs index b39a4dd..084637a 100644 --- a/src/trait_/debug.rs +++ b/src/trait_/debug.rs @@ -79,7 +79,7 @@ impl TraitImpl for Debug { SimpleType::Unit(_) => { quote! { #self_pattern => ::core::fmt::Formatter::write_str(__f, #debug_name), } } - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/default.rs b/src/trait_/default.rs index 09e72b7..af25617 100644 --- a/src/trait_/default.rs +++ b/src/trait_/default.rs @@ -60,7 +60,7 @@ impl TraitImpl for Default { SimpleType::Unit(_) => { quote! { #path } } - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } // Skip `Default` implementation if variant isn't marked with a `default` attribute. diff --git a/src/trait_/hash.rs b/src/trait_/hash.rs index f4d6e1c..5c130cf 100644 --- a/src/trait_/hash.rs +++ b/src/trait_/hash.rs @@ -69,7 +69,7 @@ impl TraitImpl for Hash { } } } - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/ord.rs b/src/trait_/ord.rs index e6f486b..17ae516 100644 --- a/src/trait_/ord.rs +++ b/src/trait_/ord.rs @@ -57,7 +57,7 @@ impl TraitImpl for Ord { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/partial_eq.rs b/src/trait_/partial_eq.rs index 9933146..b8917ad 100644 --- a/src/trait_/partial_eq.rs +++ b/src/trait_/partial_eq.rs @@ -127,7 +127,7 @@ impl TraitImpl for PartialEq { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/partial_ord.rs b/src/trait_/partial_ord.rs index 7314083..81e4849 100644 --- a/src/trait_/partial_ord.rs +++ b/src/trait_/partial_ord.rs @@ -69,7 +69,7 @@ impl TraitImpl for PartialOrd { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/zeroize.rs b/src/trait_/zeroize.rs index 3491847..dec02c3 100644 --- a/src/trait_/zeroize.rs +++ b/src/trait_/zeroize.rs @@ -138,7 +138,7 @@ impl TraitImpl for Zeroize { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } } diff --git a/src/trait_/zeroize_on_drop.rs b/src/trait_/zeroize_on_drop.rs index 8924c4a..d31b698 100644 --- a/src/trait_/zeroize_on_drop.rs +++ b/src/trait_/zeroize_on_drop.rs @@ -169,7 +169,7 @@ impl TraitImpl for ZeroizeOnDrop { } } SimpleType::Unit(_) => TokenStream::new(), - SimpleType::Union(_) => unreachable!("unexpected trait for union"), + SimpleType::Union => unreachable!("unexpected trait for union"), } } }