From 87883876bce847a7b8e16ed15a31ee62a3c129e8 Mon Sep 17 00:00:00 2001 From: Christian Legnitto Date: Thu, 16 Jul 2020 07:41:09 -1000 Subject: [PATCH] Fix some clippy issues (#704) --- juniper/benches/bench.rs | 4 +- juniper/src/executor/mod.rs | 2 +- juniper/src/http/mod.rs | 2 +- juniper/src/integrations/bson.rs | 2 +- .../src/schema/translate/graphql_parser.rs | 2 +- juniper/src/types/async_await.rs | 4 +- juniper/src/types/base.rs | 6 +- juniper/src/types/containers.rs | 4 +- juniper/src/types/pointers.rs | 2 +- juniper/src/types/subscriptions.rs | 10 ++-- juniper_codegen/src/derive_enum.rs | 8 +-- juniper_codegen/src/derive_input_object.rs | 7 ++- juniper_codegen/src/graphql_union/attr.rs | 18 +----- juniper_codegen/src/graphql_union/mod.rs | 2 +- juniper_codegen/src/impl_object.rs | 11 ++-- juniper_codegen/src/impl_scalar.rs | 58 +++++++++++-------- juniper_codegen/src/result.rs | 2 +- juniper_codegen/src/util/mod.rs | 2 +- juniper_rocket_async/src/lib.rs | 2 +- 19 files changed, 73 insertions(+), 75 deletions(-) diff --git a/juniper/benches/bench.rs b/juniper/benches/bench.rs index b5360f98..52023eef 100644 --- a/juniper/benches/bench.rs +++ b/juniper/benches/bench.rs @@ -5,8 +5,8 @@ extern crate juniper; use bencher::Bencher; use juniper::{ - execute_sync, tests::model::Database, DefaultScalarValue, EmptyMutation, EmptySubscription, - RootNode, Variables, + execute_sync, tests::fixtures::starwars::model::Database, DefaultScalarValue, EmptyMutation, + EmptySubscription, RootNode, Variables, }; fn query_type_name(b: &mut Bencher) { diff --git a/juniper/src/executor/mod.rs b/juniper/src/executor/mod.rs index f9e5f465..f58aef1c 100644 --- a/juniper/src/executor/mod.rs +++ b/juniper/src/executor/mod.rs @@ -595,7 +595,7 @@ where /// Add an error to the execution engine at the current executor location pub fn push_error(&self, error: FieldError) { - self.push_error_at(error, self.location().clone()); + self.push_error_at(error, *self.location()); } /// Add an error to the execution engine at a specific location diff --git a/juniper/src/http/mod.rs b/juniper/src/http/mod.rs index 59d9cb45..e298613d 100644 --- a/juniper/src/http/mod.rs +++ b/juniper/src/http/mod.rs @@ -42,7 +42,7 @@ where { /// Returns the `operation_name` associated with this request. pub fn operation_name(&self) -> Option<&str> { - self.operation_name.as_ref().map(|oper_name| &**oper_name) + self.operation_name.as_deref() } fn variables(&self) -> Variables { diff --git a/juniper/src/integrations/bson.rs b/juniper/src/integrations/bson.rs index 202b8d7c..15d66c94 100644 --- a/juniper/src/integrations/bson.rs +++ b/juniper/src/integrations/bson.rs @@ -42,7 +42,7 @@ where fn from_input_value(v: &InputValue) -> Option { v.as_string_value() .and_then(|s| (s.parse::>().ok())) - .map(|d| UtcDateTime(d)) + .map(UtcDateTime) } fn from_str<'a>(value: ScalarToken<'a>) -> ParseScalarResult<'a, S> { diff --git a/juniper/src/schema/translate/graphql_parser.rs b/juniper/src/schema/translate/graphql_parser.rs index 67f0ae84..e642faa0 100644 --- a/juniper/src/schema/translate/graphql_parser.rs +++ b/juniper/src/schema/translate/graphql_parser.rs @@ -259,7 +259,7 @@ impl GraphQLParserTranslator { .map(|x| GraphQLParserTranslator::translate_argument(&x)) .collect() }) - .unwrap_or_else(|| Vec::new()); + .unwrap_or_else(Vec::new); ExternalField { position: Pos::default(), diff --git a/juniper/src/types/async_await.rs b/juniper/src/types/async_await.rs index c43e30d6..4151127d 100644 --- a/juniper/src/types/async_await.rs +++ b/juniper/src/types/async_await.rs @@ -237,7 +237,7 @@ where let sub_exec = executor.field_sub_executor( &response_name, f.name.item, - start_pos.clone(), + *start_pos, f.selection_set.as_ref().map(|v| &v[..]), ); let args = Arguments::new( @@ -336,7 +336,7 @@ where })); } } else if let Err(e) = sub_result { - sub_exec.push_error_at(e, start_pos.clone()); + sub_exec.push_error_at(e, *start_pos); } } else { async_values.push(AsyncValueFuture::InlineFragment2(async move { diff --git a/juniper/src/types/base.rs b/juniper/src/types/base.rs index f646f517..d7c77f73 100644 --- a/juniper/src/types/base.rs +++ b/juniper/src/types/base.rs @@ -457,7 +457,7 @@ where let sub_exec = executor.field_sub_executor( response_name, f.name.item, - start_pos.clone(), + *start_pos, f.selection_set.as_ref().map(|v| &v[..]), ); @@ -482,7 +482,7 @@ where Ok(Value::Null) if meta_field.field_type.is_non_null() => return false, Ok(v) => merge_key_into(result, response_name, v), Err(e) => { - sub_exec.push_error_at(e, start_pos.clone()); + sub_exec.push_error_at(e, *start_pos); if meta_field.field_type.is_non_null() { return false; @@ -540,7 +540,7 @@ where merge_key_into(result, &k, v); } } else if let Err(e) = sub_result { - sub_exec.push_error_at(e, start_pos.clone()); + sub_exec.push_error_at(e, *start_pos); } } else if !resolve_selection_set_into( instance, diff --git a/juniper/src/types/containers.rs b/juniper/src/types/containers.rs index bb9d643f..074901ac 100644 --- a/juniper/src/types/containers.rs +++ b/juniper/src/types/containers.rs @@ -80,7 +80,7 @@ where T: FromInputValue, S: ScalarValue, { - fn from_input_value<'a>(v: &'a InputValue) -> Option> { + fn from_input_value(v: &InputValue) -> Option> { match v { &InputValue::Null => Some(None), v => v.convert().map(Some), @@ -163,7 +163,7 @@ where T: FromInputValue, S: ScalarValue, { - fn from_input_value<'a>(v: &'a InputValue) -> Option> + fn from_input_value(v: &InputValue) -> Option> where { match *v { InputValue::List(ref ls) => { diff --git a/juniper/src/types/pointers.rs b/juniper/src/types/pointers.rs index a1c45f81..2ed2d50a 100644 --- a/juniper/src/types/pointers.rs +++ b/juniper/src/types/pointers.rs @@ -93,7 +93,7 @@ where S: ScalarValue, T: FromInputValue, { - fn from_input_value<'a>(v: &'a InputValue) -> Option> { + fn from_input_value(v: &InputValue) -> Option> { match >::from_input_value(v) { Some(v) => Some(Box::new(v)), None => None, diff --git a/juniper/src/types/subscriptions.rs b/juniper/src/types/subscriptions.rs index 0efd1b38..9ff1cdf7 100644 --- a/juniper/src/types/subscriptions.rs +++ b/juniper/src/types/subscriptions.rs @@ -293,7 +293,7 @@ where let sub_exec = executor.field_sub_executor( response_name, f.name.item, - start_pos.clone(), + *start_pos, f.selection_set.as_ref().map(|x| &x[..]), ); @@ -319,7 +319,7 @@ where } Ok(v) => merge_key_into(&mut object, response_name, v), Err(e) => { - sub_exec.push_error_at(e, start_pos.clone()); + sub_exec.push_error_at(e, *start_pos); if meta_field.field_type.is_non_null() { return Value::Null; @@ -365,7 +365,7 @@ where _ => unreachable!(), } } - Err(e) => sub_exec.push_error_at(e, start_pos.clone()), + Err(e) => sub_exec.push_error_at(e, *start_pos), } } @@ -393,7 +393,7 @@ where merge_key_into(&mut object, &k, v); } } else if let Err(e) = sub_result { - sub_exec.push_error_at(e, start_pos.clone()); + sub_exec.push_error_at(e, *start_pos); } } else if let Some(type_name) = meta_type.name() { let sub_result = instance @@ -405,7 +405,7 @@ where merge_key_into(&mut object, &k, v); } } else if let Err(e) = sub_result { - sub_exec.push_error_at(e, start_pos.clone()); + sub_exec.push_error_at(e, *start_pos); } } else { return Value::Null; diff --git a/juniper_codegen/src/derive_enum.rs b/juniper_codegen/src/derive_enum.rs index 089f744a..919d55ec 100644 --- a/juniper_codegen/src/derive_enum.rs +++ b/juniper_codegen/src/derive_enum.rs @@ -103,10 +103,10 @@ pub fn impl_enum(ast: syn::DeriveInput, error: GraphQLScope) -> syn::Result error.duplicate(duplicates.iter()), - None => {} + if let Some(duplicates) = + crate::util::duplicate::Duplicate::find_by_key(&fields, |field| &field.name) + { + error.duplicate(duplicates.iter()) } if !attrs.interfaces.is_empty() { diff --git a/juniper_codegen/src/derive_input_object.rs b/juniper_codegen/src/derive_input_object.rs index e1c15f5a..ab50cc0a 100644 --- a/juniper_codegen/src/derive_input_object.rs +++ b/juniper_codegen/src/derive_input_object.rs @@ -102,9 +102,10 @@ pub fn impl_input_object(ast: syn::DeriveInput, error: GraphQLScope) -> syn::Res error.not_empty(ast_span); } - match crate::util::duplicate::Duplicate::find_by_key(&fields, |field| &field.name) { - Some(duplicates) => error.duplicate(duplicates.iter()), - None => {} + if let Some(duplicates) = + crate::util::duplicate::Duplicate::find_by_key(&fields, |field| &field.name) + { + error.duplicate(duplicates.iter()) } if !attrs.interfaces.is_empty() { diff --git a/juniper_codegen/src/graphql_union/attr.rs b/juniper_codegen/src/graphql_union/attr.rs index 0c9ebc32..1cdea3e3 100644 --- a/juniper_codegen/src/graphql_union/attr.rs +++ b/juniper_codegen/src/graphql_union/attr.rs @@ -36,13 +36,7 @@ pub fn expand(attr_args: TokenStream, body: TokenStream) -> syn::Result Result arg.ty.deref(), None => return Ok(None), _ => return Err(sig.inputs.span()), diff --git a/juniper_codegen/src/graphql_union/mod.rs b/juniper_codegen/src/graphql_union/mod.rs index 59f066d6..a8658132 100644 --- a/juniper_codegen/src/graphql_union/mod.rs +++ b/juniper_codegen/src/graphql_union/mod.rs @@ -696,7 +696,7 @@ fn emerge_union_variants_from_meta( /// /// [1]: https://spec.graphql.org/June2018/#sec-Unions /// [2]: https://docs.rs/static_assertions/latest/static_assertions/macro.assert_type_ne_all.html -fn all_variants_different(variants: &Vec) -> bool { +fn all_variants_different(variants: &[UnionVariantDefinition]) -> bool { let mut types: Vec<_> = variants.iter().map(|var| &var.ty).collect(); types.dedup(); types.len() == variants.len() diff --git a/juniper_codegen/src/impl_object.rs b/juniper_codegen/src/impl_object.rs index b8e0d3f9..fd824700 100644 --- a/juniper_codegen/src/impl_object.rs +++ b/juniper_codegen/src/impl_object.rs @@ -14,7 +14,7 @@ pub fn build_object(args: TokenStream, body: TokenStream, error: GraphQLScope) - Ok(definition) => definition, Err(err) => return err.to_compile_error(), }; - definition.into_tokens().into() + definition.into_tokens() } /// Generate code for the juniper::graphql_subscription macro. @@ -27,7 +27,7 @@ pub fn build_subscription( Ok(definition) => definition, Err(err) => return err.to_compile_error(), }; - definition.into_subscription_tokens().into() + definition.into_subscription_tokens() } fn create( @@ -172,9 +172,10 @@ fn create( // Early abort after checking all fields proc_macro_error::abort_if_dirty(); - match crate::util::duplicate::Duplicate::find_by_key(&fields, |field| &field.name) { - Some(duplicates) => error.duplicate(duplicates.iter()), - None => {} + if let Some(duplicates) = + crate::util::duplicate::Duplicate::find_by_key(&fields, |field| &field.name) + { + error.duplicate(duplicates.iter()) } if !_impl.attrs.is_internal && name.starts_with("__") { diff --git a/juniper_codegen/src/impl_scalar.rs b/juniper_codegen/src/impl_scalar.rs index e9ee8cd5..560dea93 100644 --- a/juniper_codegen/src/impl_scalar.rs +++ b/juniper_codegen/src/impl_scalar.rs @@ -183,37 +183,45 @@ pub fn build_scalar( let attrs = syn::parse2::(attributes)?; let input = syn::parse2::(body)?; - let impl_for_type = input.impl_for_type.ok_or(error.custom_error( - body_span, - "unable to find target for implementation target for `GraphQLScalar`", - ))?; + let impl_for_type = input.impl_for_type.ok_or_else(|| { + error.custom_error( + body_span, + "unable to find target for implementation target for `GraphQLScalar`", + ) + })?; let custom_data_type = input .custom_data_type - .ok_or(error.custom_error(body_span, "unable to find custom scalar data type"))?; + .ok_or_else(|| error.custom_error(body_span, "unable to find custom scalar data type"))?; let resolve_body = input .resolve_body - .ok_or(error.custom_error(body_span, "unable to find body of `resolve` method"))?; - let from_input_value_arg = input.from_input_value_arg.ok_or(error.custom_error( - body_span, - "unable to find argument for `from_input_value` method", - ))?; - let from_input_value_body = input.from_input_value_body.ok_or(error.custom_error( - body_span, - "unable to find body of `from_input_value` method", - ))?; - let from_input_value_result = input.from_input_value_result.ok_or(error.custom_error( - body_span, - "unable to find return type of `from_input_value` method", - ))?; - let from_str_arg = input - .from_str_arg - .ok_or(error.custom_error(body_span, "unable to find argument for `from_str` method"))?; + .ok_or_else(|| error.custom_error(body_span, "unable to find body of `resolve` method"))?; + let from_input_value_arg = input.from_input_value_arg.ok_or_else(|| { + error.custom_error( + body_span, + "unable to find argument for `from_input_value` method", + ) + })?; + let from_input_value_body = input.from_input_value_body.ok_or_else(|| { + error.custom_error( + body_span, + "unable to find body of `from_input_value` method", + ) + })?; + let from_input_value_result = input.from_input_value_result.ok_or_else(|| { + error.custom_error( + body_span, + "unable to find return type of `from_input_value` method", + ) + })?; + let from_str_arg = input.from_str_arg.ok_or_else(|| { + error.custom_error(body_span, "unable to find argument for `from_str` method") + })?; let from_str_body = input .from_str_body - .ok_or(error.custom_error(body_span, "unable to find body of `from_str` method"))?; - let from_str_result = input - .from_str_result - .ok_or(error.custom_error(body_span, "unable to find return type of `from_str` method"))?; + .ok_or_else(|| error.custom_error(body_span, "unable to find body of `from_str` method"))?; + let from_str_result = input.from_str_result.ok_or_else(|| { + error.custom_error(body_span, "unable to find return type of `from_str` method") + })?; let name = attrs .name diff --git a/juniper_codegen/src/result.rs b/juniper_codegen/src/result.rs index 73a49596..76705143 100644 --- a/juniper_codegen/src/result.rs +++ b/juniper_codegen/src/result.rs @@ -6,7 +6,7 @@ use proc_macro_error::{Diagnostic, Level}; use std::fmt; /// URL of the GraphQL specification (June 2018 Edition). -pub const SPEC_URL: &'static str = "https://spec.graphql.org/June2018/"; +pub const SPEC_URL: &str = "https://spec.graphql.org/June2018/"; #[allow(unused_variables)] pub enum GraphQLScope { diff --git a/juniper_codegen/src/util/mod.rs b/juniper_codegen/src/util/mod.rs index f35e4395..a920e010 100644 --- a/juniper_codegen/src/util/mod.rs +++ b/juniper_codegen/src/util/mod.rs @@ -600,7 +600,7 @@ impl FieldAttributes { let doc_comment = get_doc_comment(&attrs); let deprecation = get_deprecated(&attrs); - let attr_opt = attrs.into_iter().find(|attr| attr.path.is_ident("graphql")); + let attr_opt = attrs.iter().find(|attr| attr.path.is_ident("graphql")); let mut output = match attr_opt { Some(attr) => attr.parse_args()?, diff --git a/juniper_rocket_async/src/lib.rs b/juniper_rocket_async/src/lib.rs index df86417b..995e2a4f 100644 --- a/juniper_rocket_async/src/lib.rs +++ b/juniper_rocket_async/src/lib.rs @@ -257,7 +257,7 @@ where } _ => { if strict { - return Err(format!("Prohibited extra field '{}'", key).to_owned()); + return Err(format!("Prohibited extra field '{}'", key)); } } }