Skip to content

Commit

Permalink
Merge pull request #99 from ModProg/clippy
Browse files Browse the repository at this point in the history
Clippy + Typos
  • Loading branch information
ModProg authored Aug 4, 2024
2 parents 567f20b + cafac83 commit 6d2527d
Show file tree
Hide file tree
Showing 27 changed files with 77 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ zeroize_ = { version = "1.5", package = "zeroize", default-features = false }
[package.metadata.docs.rs]
all-features = true
targets = []

[workspace.metadata.typos]
default.extend-words.wheres = "wheres"
31 changes: 16 additions & 15 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,6 +29,7 @@ pub struct Data<'a> {
pub path: Path,
/// [Type](DataType) of this struct, union or variant.
pub type_: DataType<'a>,
#[cfg(not(feature = "nightly"))]
/// Discriminant of this variant.
pub discriminant: Option<&'a Expr>,
}
Expand Down Expand Up @@ -69,7 +72,7 @@ pub enum SimpleType<'a> {
/// Tuple struct or tuple variant.
Tuple(&'a Fields<'a>),
/// Union.
Union(&'a Fields<'a>),
Union,
/// Unit variant.
Unit(&'a Pat),
}
Expand Down Expand Up @@ -100,6 +103,7 @@ impl<'a> Data<'a> {
ident,
path,
type_: DataType::Struct(fields),
#[cfg(not(feature = "nightly"))]
discriminant: None,
})
}
Expand All @@ -117,6 +121,7 @@ impl<'a> Data<'a> {
ident,
path,
type_: DataType::Tuple(fields),
#[cfg(not(feature = "nightly"))]
discriminant: None,
})
}
Expand All @@ -131,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 @@ -158,6 +164,7 @@ impl<'a> Data<'a> {
ident,
path,
type_: DataType::Union(fields),
#[cfg(not(feature = "nightly"))]
discriminant: None,
})
}
Expand Down Expand Up @@ -191,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 @@ -207,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 @@ -226,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 @@ -327,15 +337,12 @@ impl<'a> Data<'a> {
type_: VariantType::Unit(pattern),
..
} => SimpleType::Unit(pattern),
DataType::Union(fields) => SimpleType::Union(fields),
DataType::Union(_) => SimpleType::Union,
}
}

/// Returns an [`Iterator`] over [`Field`]s.
pub fn iter_fields(
&self,
trait_: Trait,
) -> impl '_ + Iterator<Item = &'_ Field> + DoubleEndedIterator {
pub fn iter_fields(&self, trait_: Trait) -> impl '_ + DoubleEndedIterator<Item = &'_ Field> {
if self.skip(trait_) {
[].iter()
} else {
Expand All @@ -354,19 +361,13 @@ impl<'a> Data<'a> {

/// Returns an [`Iterator`] over [`struct@Ident`]s used as temporary
/// variables for destructuring `self`.
pub fn iter_self_ident(
&self,
trait_: Trait,
) -> impl Iterator<Item = &'_ Ident> + DoubleEndedIterator {
pub fn iter_self_ident(&self, trait_: Trait) -> impl DoubleEndedIterator<Item = &'_ Ident> {
self.iter_fields(trait_).map(|field| &field.self_ident)
}

/// Returns an [`Iterator`] over [`struct@Ident`]s used as temporary
/// variables for destructuring `other`.
pub fn iter_other_ident(
&self,
trait_: Trait,
) -> impl Iterator<Item = &'_ Ident> + DoubleEndedIterator {
pub fn iter_other_ident(&self, trait_: Trait) -> impl DoubleEndedIterator<Item = &'_ Ident> {
self.iter_fields(trait_).map(|field| &field.other_ident)
}
}
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
20 changes: 16 additions & 4 deletions src/item.rs
Original file line number Diff line number Diff line change
@@ -1,17 +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(not(feature = "nightly"))]
/// Type of discriminant used.
discriminant: Discriminant,
/// [`struct@Ident`] of this enum.
Expand Down Expand Up @@ -96,6 +103,7 @@ impl Item<'_> {
/// Type of discriminant used.
#[derive(Clone, Copy)]
#[cfg_attr(test, derive(Debug))]
#[cfg(not(feature = "nightly"))]
pub enum Discriminant {
/// The enum has only a single variant.
Single,
Expand All @@ -109,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 @@ -165,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 @@ -192,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 @@ -245,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
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
//!
//! ## Generic type bounds
//!
//! Separated from the list of traits with a semi-colon, types to bind to can be
//! Separated from the list of traits with a semicolon, types to bind to can be
//! specified. This example will restrict the implementation for `Example` to
//! `T: Clone`:
//!
Expand Down Expand Up @@ -307,7 +307,7 @@
//! ## Supported items
//!
//! Structs, tuple structs, unions and enums are supported. Derive-where tries
//! it's best to discourage usage that could be covered by std's `derive`. For
//! its best to discourage usage that could be covered by std's `derive`. For
//! example unit structs and enums only containing unit variants aren't
//! supported.
//!
Expand Down 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 Expand Up @@ -588,7 +590,7 @@ pub fn derive_where_actual(input: proc_macro::TokenStream) -> proc_macro::TokenS
clean_item.span()
};

match { Input::from_input(span, &item) } {
match Input::from_input(span, &item) {
Ok(Input {
derive_wheres,
generics,
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
1 change: 1 addition & 0 deletions tests/bound.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused)]
mod util;

use std::marker::PhantomData;
Expand Down
1 change: 1 addition & 0 deletions tests/cfg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unexpected_cfgs, unused)]
use std::marker::PhantomData;

use derive_where::derive_where;
Expand Down
2 changes: 1 addition & 1 deletion tests/hygiene.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::clone_on_copy)]
#![allow(clippy::clone_on_copy, unused)]

mod util;

Expand Down
1 change: 1 addition & 0 deletions tests/incomparable.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused)]
use derive_where::derive_where;

macro_rules! incomparable {
Expand Down
Loading

0 comments on commit 6d2527d

Please sign in to comment.