Skip to content

Commit d71d3bb

Browse files
kirushikascjones
authored andcommitted
Fix most clippy warnings (paritytech#392)
* Get rid of scary conversions in to_delegate.rs * Fix minor Clippy complaints * Update derive/src/to_delegate.rs Co-Authored-By: kirushik <[email protected]>
1 parent 4db3798 commit d71d3bb

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

derive/src/rpc_attr.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl RpcMethodAttribute {
7373
.map_or(Vec::new(), |ml| get_aliases(ml));
7474
Ok(RpcMethodAttribute {
7575
attr: attr.clone(),
76-
name: name.into(),
76+
name: name,
7777
aliases,
7878
kind
7979
})
@@ -106,7 +106,7 @@ impl RpcMethodAttribute {
106106
Err(Error::new_spanned(meta, NEITHER_SUB_OR_UNSUB_ERR)),
107107
};
108108
kind.map(|kind| AttributeKind::PubSub {
109-
subscription_name: sub_name.into(),
109+
subscription_name: sub_name,
110110
kind,
111111
})
112112
})
@@ -157,16 +157,16 @@ fn validate_attribute_meta(meta: syn::Meta) -> Result<syn::Meta> {
157157

158158
fn validate_idents(meta: &syn::Meta, attr_idents: &[String], valid: &[&str]) -> Result<syn::Meta> {
159159
let invalid_meta_words: Vec<_> = attr_idents
160-
.into_iter()
160+
.iter()
161161
.filter(|w| !valid.iter().any(|v| v == w))
162162
.cloned()
163163
.collect();
164-
if !invalid_meta_words.is_empty() {
164+
if invalid_meta_words.is_empty() {
165+
Ok(meta.clone())
166+
} else {
165167
let expected = format!("Expected '{}'", valid.join(", "));
166168
let msg = format!("{} '{}'. {}", INVALID_ATTR_PARAM_NAMES_ERR, invalid_meta_words.join(", "), expected);
167169
Err(Error::new_spanned(meta, msg))
168-
} else {
169-
Ok(meta.clone())
170170
}
171171
}
172172

@@ -203,7 +203,7 @@ fn has_meta_word(word: &str, ml: &syn::MetaList) -> bool {
203203
.iter()
204204
.any(|nested|
205205
if let syn::NestedMeta::Meta(syn::Meta::Word(w)) = nested {
206-
word == w.to_string()
206+
w == word
207207
} else {
208208
false
209209
}

derive/src/rpc_trait.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ struct RpcTrait {
2828

2929
impl<'a> Fold for RpcTrait {
3030
fn fold_trait_item_method(&mut self, method: syn::TraitItemMethod) -> syn::TraitItemMethod {
31-
let mut method = method.clone();
32-
let method_item = method.clone();
31+
let mut foldable_method = method.clone();
3332
// strip rpc attributes
34-
method.attrs.retain(|a| {
33+
foldable_method.attrs.retain(|a| {
3534
let rpc_method =
36-
self.methods.iter().find(|m| m.trait_item == method_item);
35+
self.methods.iter().find(|m| m.trait_item == method);
3736
rpc_method.map_or(true, |rpc| rpc.attr.attr != *a)
3837
});
39-
fold::fold_trait_item_method(self, method)
38+
fold::fold_trait_item_method(self, foldable_method)
4039
}
4140

4241
fn fold_trait_item_type(&mut self, ty: syn::TraitItemType) -> syn::TraitItemType {
@@ -59,10 +58,10 @@ fn generate_rpc_item_trait(item_trait: &syn::ItemTrait) -> Result<(syn::ItemTrai
5958
.iter()
6059
.filter_map(|trait_item| {
6160
if let syn::TraitItem::Method(method) = trait_item {
62-
match RpcMethodAttribute::parse_attr(&method) {
61+
match RpcMethodAttribute::parse_attr(method) {
6362
Ok(Some(attr)) =>
6463
Some(Ok(RpcMethod::new(
65-
attr.clone(),
64+
attr,
6665
method.clone(),
6766
))),
6867
Ok(None) => None, // non rpc annotated trait method
@@ -150,7 +149,7 @@ fn rpc_wrapper_mod_name(rpc_trait: &syn::ItemTrait) -> syn::Ident {
150149
pub fn rpc_impl(input: syn::Item) -> Result<proc_macro2::TokenStream> {
151150
let rpc_trait = match input {
152151
syn::Item::Trait(item_trait) => item_trait,
153-
item @ _ => return Err(syn::Error::new_spanned(item, "The #[rpc] custom attribute only works with trait declarations")),
152+
item => return Err(syn::Error::new_spanned(item, "The #[rpc] custom attribute only works with trait declarations")),
154153
};
155154

156155
let (rpc_trait, has_pubsub_methods) = generate_rpc_item_trait(&rpc_trait)?;

derive/src/to_delegate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const SUBCRIBER_TYPE_IDENT: &str = "Subscriber";
7575
const METADATA_CLOSURE_ARG: &str = "meta";
7676
const SUBSCRIBER_CLOSURE_ARG: &str = "subscriber";
7777

78+
const TUPLE_FIELD_NAMES: [&str; 26] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
79+
7880
pub fn generate_trait_item_method(
7981
methods: &[MethodRegistration],
8082
trait_item: &syn::ItemTrait,
@@ -169,9 +171,7 @@ impl RpcMethod {
169171
special_args.iter().find(|(_,sty)| sty == ty).is_none());
170172

171173
let tuple_fields : &Vec<_> =
172-
&(0..param_types.len() as u8)
173-
.map(|x| ident(&((x + 'a' as u8) as char).to_string()))
174-
.collect();
174+
&(TUPLE_FIELD_NAMES.iter().take(param_types.len()).map(|name| ident(name)).collect());
175175
let param_types = &param_types;
176176
let parse_params = {
177177
// last arguments that are `Option`-s are optional 'trailing' arguments
@@ -235,7 +235,7 @@ impl RpcMethod {
235235
let meta_arg =
236236
param_types.first().and_then(|ty|
237237
if *ty == parse_quote!(Self::Metadata) { Some(ty.clone()) } else { None });
238-
let subscriber_arg = param_types.iter().nth(1).and_then(|ty| {
238+
let subscriber_arg = param_types.get(1).and_then(|ty| {
239239
if let syn::Type::Path(path) = ty {
240240
if path.path.segments.iter().any(|s| s.ident == SUBCRIBER_TYPE_IDENT) {
241241
Some(ty.clone())

0 commit comments

Comments
 (0)