Skip to content

Commit 95796c7

Browse files
committed
Appeae Clippy
1 parent b1b439b commit 95796c7

7 files changed

Lines changed: 46 additions & 42 deletions

File tree

struct-to-enum-macros/src/common.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use proc_macro2::{Span, TokenStream as TokenStream2};
2-
use quote::ToTokens;
32
#[cfg(any(feature = "nested-type", feature = "nested-name"))]
43
use quote::quote;
4+
use quote::ToTokens;
55
use syn::{Attribute, Fields, Ident, Meta, Type};
66

77
use heck::ToUpperCamelCase;
@@ -61,7 +61,7 @@ pub fn filter_fields(
6161
derive_type: DeriveVariant,
6262
) -> syn::Result<Vec<FieldInfo>> {
6363
let mut result = Vec::new();
64-
for field in fields.iter() {
64+
for field in fields {
6565
let is_skip = field
6666
.attrs
6767
.iter()
@@ -131,7 +131,7 @@ fn get_attr_value(attr: &Attribute, attr_names: &[&str]) -> syn::Result<Option<S
131131
// #[... = "skip"] value is an expression
132132
Meta::NameValue(name_value) => name_value.value.to_token_stream().to_string(),
133133

134-
_ => {
134+
Meta::Path(_) => {
135135
return Err(syn::Error::new_spanned(meta, "Unknown attribute format"));
136136
}
137137
};
@@ -145,8 +145,7 @@ fn has_attr_with_value(attr: &Attribute, attr_names: &[&str], expected: &str) ->
145145
get_attr_value(attr, attr_names)
146146
.ok()
147147
.flatten()
148-
.map(|v| v == expected)
149-
.unwrap_or(false)
148+
.is_some_and(|v| v == expected)
150149
}
151150

152151
/// Extract the type name from a path

struct-to-enum-macros/src/field_name.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use syn::{DeriveInput, Ident, Path};
55

66
const DEFAULT_DERIVES: &[&str] = &["Debug", "PartialEq", "Eq", "Clone", "Copy"];
77

8-
use crate::common::{DeriveVariant, filter_fields, get_meta_list, path_to_string};
98
#[cfg(feature = "nested-name")]
109
use crate::common::{extract_type_ident, macro_rules_field_counter};
10+
use crate::common::{filter_fields, get_meta_list, path_to_string, DeriveVariant};
1111

1212
fn get_helper_macro_name(type_snake: &str) -> Ident {
1313
Ident::new(
14-
&format!("__{}_field_name_variants", type_snake),
14+
&format!("__{type_snake}_field_name_variants"),
1515
Span::call_site(),
1616
)
1717
}
@@ -23,7 +23,7 @@ struct FieldNamePair {
2323

2424
/// A single field slot in declaration order
2525
enum FieldSlot {
26-
/// One or more consecutive regular fields: (variant_ident, field_name)
26+
/// One or more consecutive regular fields: (`variant_ident`, `field_name`)
2727
Regular(Vec<FieldNamePair>),
2828
/// A nested field - calls to the inner type's helper macro
2929
#[cfg(feature = "nested-name")]
@@ -61,7 +61,7 @@ pub struct DeriveFieldName {
6161
}
6262

6363
impl DeriveFieldName {
64-
/// Parses DeriveInput and collects data into [`DeriveFieldName`]
64+
/// Parses `DeriveInput` and collects data into [`DeriveFieldName`]
6565
pub fn new(input: DeriveInput) -> syn::Result<Self> {
6666
let vis = input.vis;
6767
let ident = input.ident;
@@ -105,7 +105,7 @@ impl DeriveFieldName {
105105
continue;
106106
}
107107
let pair = FieldNamePair {
108-
variant_ident: f.variant_ident.to_owned(),
108+
variant_ident: f.variant_ident.clone(),
109109
field_name: f.field_ident.to_string(),
110110
};
111111
if let Some(FieldSlot::Regular(pairs)) = slots.last_mut() {
@@ -129,7 +129,7 @@ impl DeriveFieldName {
129129

130130
/// Generates the derived `FieldName` enum and its implmentations
131131
/// uses either simple or nested expansion path.
132-
pub fn expand(self) -> syn::Result<TokenStream2> {
132+
pub fn expand(self) -> TokenStream2 {
133133
#[cfg(feature = "nested-name")]
134134
{
135135
let has_nested = self
@@ -149,7 +149,7 @@ impl DeriveFieldName {
149149
}
150150

151151
/// Returns the flat list of `FieldNamePair`s
152-
/// Only valid when there are no nested slots (expant_simple)
152+
/// Only valid when there are no nested slots (expand simple)
153153
fn get_fields_for_simple(&self) -> &[FieldNamePair] {
154154
//PERF: this function is called several times instead of once
155155
// debug_assert_eq!(self.slots.len(), 1);
@@ -161,7 +161,7 @@ impl DeriveFieldName {
161161
}
162162

163163
/// Expands with no nested fields
164-
fn expand_simple(&self) -> syn::Result<TokenStream2> {
164+
fn expand_simple(&self) -> TokenStream2 {
165165
// No nested fields- exactly one Regular slot.
166166
let pairs = self.get_fields_for_simple();
167167
let entries = FieldSlot::entries(pairs);
@@ -198,19 +198,19 @@ impl DeriveFieldName {
198198
};
199199
let variant_counter = quote! { #variant_count };
200200
let enum_def = self.gen_enum_def(derive_attrs, &variants);
201-
let field_names_impl = self.gen_field_names_impl(&constructs, variant_counter);
201+
let field_names_impl = self.gen_field_names_impl(&constructs, &variant_counter);
202202

203-
Ok(quote! {
203+
quote! {
204204
#enum_def
205205
#field_names_impl
206206
#own_helper
207-
})
207+
}
208208
}
209209

210210
/// Expands the struct with one or more nested fields using a chain of step macros
211211
/// that accumulate `Variant => "name"` pairs
212212
#[cfg(feature = "nested-name")]
213-
fn expand_nested(&self) -> syn::Result<TokenStream2> {
213+
fn expand_nested(&self) -> TokenStream2 {
214214
let type_snake = &self.type_snake;
215215

216216
let builder_macro_name = Ident::new(
@@ -275,12 +275,12 @@ impl DeriveFieldName {
275275
}
276276
};
277277

278-
Ok(quote! {
278+
quote! {
279279
#builder_macro
280280
#(#step_macros)*
281281
#own_helper
282282
#invocation
283-
})
283+
}
284284
}
285285

286286
/// Emit the builder macro that, once it has the full flat list of
@@ -294,7 +294,7 @@ impl DeriveFieldName {
294294
let variant_counter = macro_rules_field_counter();
295295
let enum_def = self.gen_enum_def(enum_derives, &[quote!($($variant),*)]);
296296
let field_names_impl =
297-
self.gen_field_names_impl(&[quote!($(#enum_ty::$variant),*)], variant_counter);
297+
self.gen_field_names_impl(&[quote!($(#enum_ty::$variant),*)], &variant_counter);
298298

299299
quote! {
300300
#[doc(hidden)]
@@ -322,11 +322,11 @@ impl DeriveFieldName {
322322
}
323323

324324
/// Generates the `impl FieldNames<N> for OriginalStruct` block
325-
/// variant_counter has to be `Tokenstream2` because nested enum field names aren't known at derive macro level
325+
/// `variant_counter` has to be `Tokenstream2` because nested enum field names aren't known at derive macro level
326326
fn gen_field_names_impl(
327327
&self,
328328
constructs: &[TokenStream2],
329-
variant_counter: TokenStream2,
329+
variant_counter: &TokenStream2,
330330
) -> TokenStream2 {
331331
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
332332
let ident = &self.ident;

struct-to-enum-macros/src/field_type.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use proc_macro2::{Span, TokenStream as TokenStream2};
44
use quote::quote;
55
use syn::{DeriveInput, Ident, Type};
66

7-
use crate::common::{DeriveVariant, filter_fields, get_meta_list};
87
#[cfg(feature = "nested-type")]
98
use crate::common::{extract_type_ident, macro_rules_field_counter};
9+
use crate::common::{filter_fields, get_meta_list, DeriveVariant};
1010

1111
#[cfg(feature = "nested-type")]
1212
fn get_helper_macro_name(type_snake: &str) -> Ident {
@@ -40,7 +40,7 @@ impl std::fmt::Debug for NormalField {
4040

4141
/// A single field slot in declaration order
4242
enum FieldSlot {
43-
/// One or more consecutive regular fields: (variant_ident, field_ident, field_type)
43+
/// One or more consecutive regular fields: (`variant_ident`, `field_ident`, `field_ty`)
4444
Regular(Vec<NormalField>),
4545
/// A nested field - calls to the inner type's helper macro
4646
#[cfg(feature = "nested-type")]
@@ -130,7 +130,7 @@ impl DeriveFieldType {
130130
})
131131
}
132132

133-
pub fn expand(self) -> syn::Result<TokenStream2> {
133+
pub fn expand(self) -> TokenStream2 {
134134
#[cfg(feature = "nested-type")]
135135
{
136136
let has_nested = self
@@ -149,7 +149,7 @@ impl DeriveFieldType {
149149
}
150150
}
151151

152-
fn expand_simple(self) -> syn::Result<TokenStream2> {
152+
fn expand_simple(self) -> TokenStream2 {
153153
let enum_def = self.enum_definition();
154154
let converter = self.converter_impl();
155155

@@ -175,18 +175,19 @@ impl DeriveFieldType {
175175
};
176176
}
177177
};
178-
Ok(quote! {
178+
179+
return quote! {
179180
#enum_def
180181
#converter
181182
#own_helper
182-
})
183+
};
183184
}
184185

185186
#[cfg(not(feature = "nested-type"))]
186-
Ok(quote! {
187+
quote! {
187188
#enum_def
188189
#converter
189-
})
190+
}
190191
}
191192

192193
fn get_fields_for_simple(&self) -> &[NormalField] {
@@ -199,7 +200,7 @@ impl DeriveFieldType {
199200
}
200201

201202
#[cfg(feature = "nested-type")]
202-
fn expand_nested(&self) -> syn::Result<TokenStream2> {
203+
fn expand_nested(&self) -> TokenStream2 {
203204
let (_, ty_generics, _) = self.generics.split_for_impl();
204205
let type_snake = &self.type_snake;
205206

@@ -283,12 +284,12 @@ impl DeriveFieldType {
283284
}
284285
};
285286

286-
Ok(quote! {
287+
quote! {
287288
#builder_macro
288289
#(#step_macros)*
289290
#own_helper
290291
#invocation
291-
})
292+
}
292293
}
293294

294295
/// Builder macro: receives all `Variant(Type) { .path },` entries

struct-to-enum-macros/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,10 @@ use syn::DeriveInput;
155155
pub fn field_type(input: TokenStream) -> TokenStream {
156156
let input = syn::parse_macro_input!(input as DeriveInput);
157157
DeriveFieldType::new(input)
158-
.and_then(|d| d.expand())
159-
.unwrap_or_else(|e| e.to_compile_error())
158+
.map_or_else(
159+
|e| e.to_compile_error(),
160+
field_type::DeriveFieldType::expand,
161+
)
160162
.into()
161163
}
162164

@@ -292,7 +294,9 @@ pub fn field_type(input: TokenStream) -> TokenStream {
292294
pub fn field_name(input: TokenStream) -> TokenStream {
293295
let input = syn::parse_macro_input!(input as DeriveInput);
294296
DeriveFieldName::new(input)
295-
.and_then(|d| d.expand())
296-
.unwrap_or_else(|e| e.to_compile_error())
297+
.map_or_else(
298+
|e| e.to_compile_error(),
299+
field_name::DeriveFieldName::expand,
300+
)
297301
.into()
298302
}

test-suite/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "struct-to-enum-tests"
33
version = "0.0.0"
44
authors = ["Sinder <smirnov.sinder@gmail.com>"]
5-
edition = "2024"
5+
edition = "2021"
66
publish = false
77

88

test-suite/tests/field_name/nested.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,13 @@ mod mixed_skip_nested_mod {
148148
pub inner: InnerMixed,
149149
#[stem_name(skip)]
150150
pub c: i32,
151+
151152
pub d: i32,
152153
}
153154
}
154155

155156
use mixed_skip_nested_mod::{Mixed, MixedFieldName};
156157

157-
use crate::nested::complex_nesting::{ABCDEFGHIJKLMNOP, ABCDEFGHIJKLMNOPFieldName};
158-
159158
// Complex nesting
160159
mod complex_nesting {
161160
use struct_to_enum::FieldName;
@@ -233,6 +232,7 @@ mod complex_nesting {
233232
pub ghijklmnop: GHIJKLMNOP,
234233
}
235234
}
235+
use crate::nested::complex_nesting::{ABCDEFGHIJKLMNOPFieldName, ABCDEFGHIJKLMNOP};
236236

237237
#[test]
238238
fn complex_fields_field_name() {

test-suite/tests/field_type/nested.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ struct MixedType {
169169
d: i32,
170170
}
171171

172-
use crate::nested::complex_nesting::{ABCDEFGHIJKLMNOP, ABCDEFGHIJKLMNOPFieldType};
173-
174172
// Complex nesting
175173
mod complex_nesting {
176174
use struct_to_enum::FieldType;
@@ -259,6 +257,8 @@ mod complex_nesting {
259257
}
260258
}
261259

260+
use crate::nested::complex_nesting::{ABCDEFGHIJKLMNOPFieldType, ABCDEFGHIJKLMNOP};
261+
262262
#[test]
263263
fn complex_fields_field_type() {
264264
use ABCDEFGHIJKLMNOPFieldType as AlpName;

0 commit comments

Comments
 (0)