From be545a874a3ec58b5a49612239c3e62af145f66e Mon Sep 17 00:00:00 2001 From: Seyyed Morteza Moosavi Date: Fri, 31 May 2024 02:11:32 +0330 Subject: [PATCH] Fix unused errors in tests (#32) * fix unused error in app.rs * fix unused error in scalar.rs * fix unused error in expand object.rs * fix dead code warnings --- derive/src/args/app.rs | 8 +++++++ derive/src/args/common.rs | 2 ++ derive/src/args/common/clippy_error.rs | 29 +++++++++++++++++++++++ derive/src/args/expand_object.rs | 8 +++++++ derive/src/args/scalar.rs | 8 +++++++ derive/tests/app.rs | 4 ++++ derive/tests/interface/interface_tests.rs | 1 + 7 files changed, 60 insertions(+) create mode 100644 derive/src/args/common/clippy_error.rs diff --git a/derive/src/args/app.rs b/derive/src/args/app.rs index 49d0768..8f01555 100644 --- a/derive/src/args/app.rs +++ b/derive/src/args/app.rs @@ -7,8 +7,14 @@ use crate::utils::crate_name::get_crate_name; use crate::utils::derive_types::BaseStruct; use crate::utils::derive_types::TupleField; +use super::common::impl_suppress_tupple_clippy_error; + pub type App = BaseStruct; +fn impl_suppress_clippy_error(app: &App) -> TokenStream { + impl_suppress_tupple_clippy_error(&app.ident, &app.generics, app.data.fields.len()) +} + fn impl_create_schema(app: &App) -> TokenStream { let crate_name = get_crate_name(); let ident = &app.ident; @@ -54,9 +60,11 @@ impl ToTokens for App { fn to_tokens(&self, tokens: &mut TokenStream) { let impl_register = impl_register(self); let impl_create_schema = impl_create_schema(self); + let impl_suppress_clippy_error = impl_suppress_clippy_error(self); tokens.extend(quote! { #impl_register #impl_create_schema + #impl_suppress_clippy_error }); } } diff --git a/derive/src/args/common.rs b/derive/src/args/common.rs index bc1a38e..003e0d8 100644 --- a/derive/src/args/common.rs +++ b/derive/src/args/common.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; pub use args::*; +pub use clippy_error::impl_suppress_tupple_clippy_error; pub use fields::*; pub use generics::*; pub use interfaces::*; @@ -22,6 +23,7 @@ use crate::utils::rename_rule::calc_type_name; use crate::utils::type_utils::get_owned_type; mod args; +mod clippy_error; mod fields; mod generics; mod interfaces; diff --git a/derive/src/args/common/clippy_error.rs b/derive/src/args/common/clippy_error.rs new file mode 100644 index 0000000..a1d6b1b --- /dev/null +++ b/derive/src/args/common/clippy_error.rs @@ -0,0 +1,29 @@ +use quote::quote; + +use proc_macro2::TokenStream; +use syn::Generics; + +pub fn impl_suppress_tupple_clippy_error( + ident: &syn::Ident, + generics: &Generics, + len: usize, +) -> TokenStream { + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); + let accesses: TokenStream = (0..len) + .map(|index| { + let index = syn::Index::from(index); + quote! { + let _ = self.#index; + } + }) + .collect(); + quote! { + impl #impl_generics #ident #ty_generics #where_clause { + #[allow(dead_code)] + #[doc(hidden)] + fn _suppress_clippy_error(&self) { + #accesses + } + } + } +} diff --git a/derive/src/args/expand_object.rs b/derive/src/args/expand_object.rs index 16cd6b4..817c7e5 100644 --- a/derive/src/args/expand_object.rs +++ b/derive/src/args/expand_object.rs @@ -14,6 +14,8 @@ use crate::utils::register_attr::RegisterAttr; use crate::utils::type_utils::get_owned_type; use crate::utils::with_attributes::WithAttributes; +use super::common::impl_suppress_tupple_clippy_error; + #[derive(FromAttributes, Debug, Clone)] #[darling(attributes(graphql))] pub struct ExpandObjectAttrs { @@ -120,6 +122,10 @@ fn impl_register_fns_trait(object: &impl CommonObject) -> darling::Result TokenStream { + impl_suppress_tupple_clippy_error(&expand_object.ident, &expand_object.generics, 1) +} + impl ToTokens for ExpandObject { fn to_tokens(&self, tokens: &mut TokenStream) { let impl_expand_object = impl_expand_object(self).into_token_stream(); @@ -127,12 +133,14 @@ impl ToTokens for ExpandObject { let impl_from = impl_from(self).into_token_stream(); let impl_register_fns_trait = impl_register_fns_trait(self).into_token_stream(); let impl_registers_fn = impl_registers_fn(self).into_token_stream(); + let impl_suppress = impl_suppress_clippy_error(self); tokens.extend(quote! { #impl_registers_fn #impl_expand_object #impl_from // #impl_parent #impl_register_fns_trait + #impl_suppress }); } } diff --git a/derive/src/args/scalar.rs b/derive/src/args/scalar.rs index f6ce922..1a94424 100644 --- a/derive/src/args/scalar.rs +++ b/derive/src/args/scalar.rs @@ -20,6 +20,8 @@ use crate::utils::register_attr::RegisterAttr; use crate::utils::with_attributes::WithAttributes; use crate::utils::with_doc::WithDoc; +use super::common::impl_suppress_tupple_clippy_error; + #[derive(FromAttributes, Debug, Clone)] #[darling(attributes(graphql))] pub struct ScalarAttrs { @@ -188,6 +190,10 @@ fn impl_register(scalar: &Scalar) -> darling::Result { }) } +fn impl_suppress_clippy_error(scalar: &Scalar) -> TokenStream { + impl_suppress_tupple_clippy_error(&scalar.ident, &scalar.generics, 1) +} + impl ToTokens for Scalar { fn to_tokens(&self, tokens: &mut TokenStream) { let impl_scalar = impl_scalar(self).into_token_stream(); @@ -195,12 +201,14 @@ impl ToTokens for Scalar { let impl_resolve_ref = impl_resolve_ref(self).into_token_stream(); let impl_from_value = impl_from_value(self).into_token_stream(); let impl_register = impl_register(self).into_token_stream(); + let impl_suppress = impl_suppress_clippy_error(self); tokens.extend(quote! { #impl_scalar #impl_resolved_own #impl_resolve_ref #impl_from_value #impl_register + #impl_suppress }) } } diff --git a/derive/tests/app.rs b/derive/tests/app.rs index ce52558..62b6656 100644 --- a/derive/tests/app.rs +++ b/derive/tests/app.rs @@ -54,6 +54,7 @@ fn test_app_with_generic() { } trait GetFoo { + #[allow(dead_code)] fn get_foo(&self) -> Foo; } @@ -105,6 +106,7 @@ fn test_app_with_lifetime() { pub string: Foo, } + #[allow(dead_code)] struct Other<'a>(&'a Foo); impl<'a> Register for Other<'a> { fn register(registry: Registry) -> Registry { @@ -147,6 +149,7 @@ fn test_app_with_generic_and_lifetime() { } trait GetFoo { + #[allow(dead_code)] fn get_foo(&self) -> Foo; } @@ -158,6 +161,7 @@ fn test_app_with_generic_and_lifetime() { } } + #[allow(dead_code)] struct Other<'a, T>(&'a T) where T: GetFoo; diff --git a/derive/tests/interface/interface_tests.rs b/derive/tests/interface/interface_tests.rs index 065ab1d..38ae25c 100644 --- a/derive/tests/interface/interface_tests.rs +++ b/derive/tests/interface/interface_tests.rs @@ -295,6 +295,7 @@ fn test_schema_with_skip() { #[Interface] trait Node { fn the_id(&self) -> String; + #[allow(dead_code)] #[graphql(skip)] fn old(&self) -> String; }