From 7ed17d8951fe083829f249e9ba9fa389b4e08298 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 15 Apr 2021 23:54:28 +0300 Subject: [PATCH] chore: deny and fix all clippy rules --- itconfig-macro/src/expand.rs | 2 +- itconfig-macro/src/lib.rs | 1 + itconfig-macro/src/parse.rs | 21 +++++++++++++-------- itconfig-macro/src/utils.rs | 9 ++++----- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/itconfig-macro/src/expand.rs b/itconfig-macro/src/expand.rs index 50196c3..7008088 100644 --- a/itconfig-macro/src/expand.rs +++ b/itconfig-macro/src/expand.rs @@ -125,7 +125,7 @@ impl ToTokens for Variable { let env_name = &self .env_name .clone() - .unwrap_or(name.to_string().to_uppercase()); + .unwrap_or_else(|| name.to_string().to_uppercase()); let meta = vec_to_token_stream_2(&self.meta); let get_variable: TokenStream2 = if self.concat_parts.is_some() { diff --git a/itconfig-macro/src/lib.rs b/itconfig-macro/src/lib.rs index 8c4aa63..038de09 100644 --- a/itconfig-macro/src/lib.rs +++ b/itconfig-macro/src/lib.rs @@ -1,4 +1,5 @@ #![recursion_limit = "256"] +#![deny(clippy::all)] #![forbid(unsafe_code)] mod ast; diff --git a/itconfig-macro/src/parse.rs b/itconfig-macro/src/parse.rs index 3cbfc17..9a1f923 100644 --- a/itconfig-macro/src/parse.rs +++ b/itconfig-macro/src/parse.rs @@ -117,7 +117,7 @@ impl Parse for RootNamespace { match attr.parse_meta()? { Meta::List(MetaList { nested, .. }) => { let message = - format!("expected #[config(name = \"...\")] or #[config(unwrap)]"); + "expected #[config(name = \"...\")] or #[config(unwrap)]".to_string(); match nested.first().unwrap() { NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, @@ -127,7 +127,7 @@ impl Parse for RootNamespace { if path.is_ident("name") { name = Some(Ident::new(&lit_str.value(), Span::call_site())); } else { - Err(Error::new_spanned(attr, message))?; + return Err(Error::new_spanned(attr, message)); } } NestedMeta::Meta(Meta::Path(path)) => { @@ -135,17 +135,17 @@ impl Parse for RootNamespace { name = None; with_module = false; } else { - Err(Error::new_spanned(attr, message))?; + return Err(Error::new_spanned(attr, message)); } } _ => { - Err(Error::new_spanned(attr, message))?; + return Err(Error::new_spanned(attr, message)); } } } _ => { - let message = format!("expected #[config(...)]"); - Err(Error::new_spanned(attr, message))?; + let message = "expected #[config(...)]".to_string(); + return Err(Error::new_spanned(attr, message)); } } } else { @@ -166,7 +166,7 @@ impl Parse for RootNamespace { let prefix = String::new(); let namespaces = namespaces .into_iter() - .map(fill_env_prefix(prefix.clone())) + .map(fill_env_prefix(prefix)) .collect(); Ok(RootNamespace { @@ -231,7 +231,10 @@ impl Parse for Variable { if content.peek(Ident::peek_any) { let concat_var: Variable = content.parse()?; let name = &concat_var.name; - let env_name = &concat_var.env_name.clone().unwrap_or(name.to_string()); + let env_name = &concat_var + .env_name + .clone() + .unwrap_or_else(|| name.to_string()); let get_variable = if concat_var.initial.is_some() { let initial = concat_var.initial.as_ref().unwrap(); @@ -245,8 +248,10 @@ impl Parse for Variable { let part: Lit = content.parse()?; tmp_vec.push(quote!(#part.to_string())); } + content.parse::().ok(); } + concat_parts = Some(tmp_vec); } else { initial = input diff --git a/itconfig-macro/src/utils.rs b/itconfig-macro/src/utils.rs index f909fbc..3d03212 100644 --- a/itconfig-macro/src/utils.rs +++ b/itconfig-macro/src/utils.rs @@ -2,7 +2,9 @@ use proc_macro2::TokenStream as TokenStream2; use quote::ToTokens; use syn::{Path, Type}; -pub fn vec_to_token_stream_2(input: &Vec) -> Vec +const OPTION_PATH_IDENTS: &[&str] = &["Option|", "std|option|Option|", "core|option|Option|"]; + +pub fn vec_to_token_stream_2(input: &[T]) -> Vec where T: ToTokens, { @@ -21,10 +23,7 @@ fn path_ident(path: &Path) -> String { } fn is_option_path_ident(path_ident: String) -> bool { - vec!["Option|", "std|option|Option|", "core|option|Option|"] - .into_iter() - .find(|s| &path_ident == *s) - .is_some() + OPTION_PATH_IDENTS.iter().any(|s| path_ident == *s) } pub fn is_option_type(ty: &Type) -> bool {