Skip to content

Commit

Permalink
Remove allow(unused) in favor of cfg guards
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored and ModProg committed Aug 4, 2024
1 parent 3ac7e36 commit a029dc6
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 22 deletions.
17 changes: 13 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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>,
}
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -101,6 +103,7 @@ impl<'a> Data<'a> {
ident,
path,
type_: DataType::Struct(fields),
#[cfg(not(feature = "nightly"))]
discriminant: None,
})
}
Expand All @@ -118,6 +121,7 @@ impl<'a> Data<'a> {
ident,
path,
type_: DataType::Tuple(fields),
#[cfg(not(feature = "nightly"))]
discriminant: None,
})
}
Expand All @@ -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)),
Expand Down Expand Up @@ -159,6 +164,7 @@ impl<'a> Data<'a> {
ident,
path,
type_: DataType::Union(fields),
#[cfg(not(feature = "nightly"))]
discriminant: None,
})
}
Expand Down Expand Up @@ -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),
})
}
Expand All @@ -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),
})
}
Expand All @@ -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),
})
}
Expand Down Expand Up @@ -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,
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -99,6 +102,7 @@ impl<'a> Input<'a> {
}

Item::Enum {
#[cfg(not(feature = "nightly"))]
discriminant,
ident,
variants,
Expand Down
22 changes: 16 additions & 6 deletions src/item.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
//! 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))]
#[allow(clippy::large_enum_variant)]
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.
Expand Down Expand Up @@ -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,
Expand All @@ -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<Variant, Token![,]>) -> Result<Self> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Self> {
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion src/trait_/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl TraitImpl for Clone {
SimpleType::Unit(pattern) => {
quote! { #pattern => #pattern, }
}
SimpleType::Union(_) => TokenStream::new(),
SimpleType::Union => TokenStream::new(),
}
}
}
2 changes: 1 addition & 1 deletion src/trait_/common_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ pub fn build_incomparable_pattern(variants: &[Data]) -> Option<TokenStream> {
.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();
Expand Down
2 changes: 1 addition & 1 deletion src/trait_/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
2 changes: 1 addition & 1 deletion src/trait_/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/trait_/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl TraitImpl for Hash {
}
}
}
SimpleType::Union(_) => unreachable!("unexpected trait for union"),
SimpleType::Union => unreachable!("unexpected trait for union"),
}
}
}
2 changes: 1 addition & 1 deletion src/trait_/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/trait_/partial_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/trait_/partial_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/trait_/zeroize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/trait_/zeroize_on_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
}
Expand Down

0 comments on commit a029dc6

Please sign in to comment.