Skip to content

Commit 352e04c

Browse files
committed
fix(rossweisse): Fix all clippy pedantic and nursery lint errors
1 parent 014bf97 commit 352e04c

4 files changed

Lines changed: 46 additions & 44 deletions

File tree

rossweisse/src/implementations/route.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717

1818
use proc_macro::TokenStream;
1919

20-
pub fn route(_arguments: TokenStream, item: syn::ItemFn) -> TokenStream {
20+
pub fn route(_arguments: TokenStream, item: &syn::ItemFn) -> TokenStream {
2121
quote::quote! { #item }.into()
2222
}

rossweisse/src/implementations/router/fields.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use proc_macro::TokenStream;
1919
use quote::quote;
20+
use syn::punctuated::Punctuated;
2021

2122
pub fn fields(arguments: TokenStream, item: syn::ItemStruct) -> TokenStream {
2223
let field_initializers = syn::parse_macro_input!(
@@ -29,11 +30,11 @@ pub fn fields(arguments: TokenStream, item: syn::ItemStruct) -> TokenStream {
2930
(
3031
syn::FieldsNamed {
3132
brace_token: syn::token::Brace::default(),
32-
named: Default::default(),
33+
named: Punctuated::default(),
3334
},
3435
false,
3536
),
36-
_ =>
37+
syn::Fields::Unnamed(_) =>
3738
panic!(
3839
"`#[rossweisse::router]` can only be used on `struct`s with named \
3940
fields or unit structs"
@@ -46,17 +47,19 @@ pub fn fields(arguments: TokenStream, item: syn::ItemStruct) -> TokenStream {
4647
.0
4748
.iter()
4849
.find(|initialiser| initialiser.ident == name.clone().unwrap())
49-
.map(|initialiser| &initialiser.expr)
50-
.unwrap_or_else(|| {
51-
default_expressions.push({
52-
let default_expression: syn::Expr =
53-
syn::parse_quote! { ::std::default::Default::default() };
50+
.map_or_else(
51+
|| {
52+
default_expressions.push({
53+
let default_expression: syn::Expr =
54+
syn::parse_quote! { ::std::default::Default::default() };
5455

55-
default_expression
56-
});
56+
default_expression
57+
});
5758

58-
default_expressions.last().unwrap()
59-
});
59+
default_expressions.last().unwrap()
60+
},
61+
|initialiser| &initialiser.expr,
62+
);
6063

6164
quote! {
6265
#name: #initialiser,

rossweisse/src/implementations/router/methods.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,25 @@ pub fn methods(
2525
.items
2626
.iter_mut()
2727
.filter_map(|item| {
28-
if let syn::ImplItem::Fn(method) = item {
29-
for attribute in method.attrs.iter() {
30-
if attribute.path().is_ident("route") {
31-
let arguments = quote::ToTokens::into_token_stream(attribute)
32-
.to_string()
33-
.trim_end_matches(")]")
34-
.trim_start_matches("#[route(")
35-
.to_string();
28+
let syn::ImplItem::Fn(method) = item else {
29+
return None;
30+
};
31+
let route_attrribute = method
32+
.attrs
33+
.iter()
34+
.find(|attribute| attribute.path().is_ident("route"))?;
35+
let arguments = quote::ToTokens::into_token_stream(route_attrribute)
36+
.to_string()
37+
.trim_end_matches(")]")
38+
.trim_start_matches("#[route(")
39+
.to_string();
3640

37-
if arguments == "index" {
38-
method.sig.ident =
39-
syn::Ident::new("__router_index", method.sig.ident.span());
40-
}
41-
42-
return Some(method.sig.ident.clone());
43-
} else {
44-
return None;
45-
}
46-
}
47-
48-
None
49-
} else {
50-
None
41+
if arguments == "index" {
42+
method.sig.ident =
43+
syn::Ident::new("__router_index", method.sig.ident.span());
5144
}
45+
46+
Some(method.sig.ident.clone())
5247
})
5348
.collect::<Vec<_>>();
5449
let (implementation_generics, type_generics, where_clause) =
@@ -60,7 +55,7 @@ pub fn methods(
6055
format!(
6156
"/{}",
6257
if route == "__router_index" {
63-
"".to_string()
58+
String::new()
6459
} else {
6560
route.to_string()
6661
}

rossweisse/src/lib.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ use syn::Item;
3636
/// Marks a `struct` as a router or marks an `impl` block as a router
3737
/// implementation
3838
///
39+
/// # Panics
40+
///
41+
/// Panics if used on an item that is not a `struct` or `impl` block.
42+
///
3943
/// # Examples
4044
///
4145
/// ```rust
@@ -57,17 +61,19 @@ use syn::Item;
5761
/// ```
5862
#[proc_macro_attribute]
5963
pub fn router(arguments: TokenStream, item: TokenStream) -> TokenStream {
60-
let output = match syn::parse::<Item>(item.clone()) {
64+
match syn::parse::<Item>(item) {
6165
Ok(Item::Struct(item)) => implementations::fields(arguments, item),
6266
Ok(Item::Impl(item)) => implementations::methods(arguments, item),
6367
_ => panic!("`#[rossweisse::router]` can only be used on `struct`s"),
64-
};
65-
66-
output.into()
68+
}
6769
}
6870

6971
/// Marks a method of a router implementation as a route to mount
7072
///
73+
/// # Panics
74+
///
75+
/// Panics if used on an item that is not a function.
76+
///
7177
/// # Examples
7278
///
7379
/// ```rust
@@ -84,10 +90,8 @@ pub fn router(arguments: TokenStream, item: TokenStream) -> TokenStream {
8490
/// ```
8591
#[proc_macro_attribute]
8692
pub fn route(arguments: TokenStream, item: TokenStream) -> TokenStream {
87-
let output = match syn::parse::<Item>(item.clone()) {
88-
Ok(Item::Fn(item)) => implementations::route(arguments, item),
93+
match syn::parse::<Item>(item) {
94+
Ok(Item::Fn(ref item)) => implementations::route(arguments, item),
8995
_ => panic!("`#[rossweisse::route]` can only be used on `fn`s"),
90-
};
91-
92-
output.into()
96+
}
9397
}

0 commit comments

Comments
 (0)