Skip to content

Commit

Permalink
Fix unused errors in tests (#32)
Browse files Browse the repository at this point in the history
* fix unused error in app.rs

* fix unused error in scalar.rs

* fix unused error in expand object.rs

* fix dead code warnings
  • Loading branch information
smmoosavi authored May 30, 2024
1 parent 8490fca commit be545a8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions derive/src/args/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TupleField, Generics>;

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;
Expand Down Expand Up @@ -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
});
}
}
2 changes: 2 additions & 0 deletions derive/src/args/common.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions derive/src/args/common/clippy_error.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
8 changes: 8 additions & 0 deletions derive/src/args/expand_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -120,19 +122,25 @@ fn impl_register_fns_trait(object: &impl CommonObject) -> darling::Result<TokenS
Ok(q)
}

fn impl_suppress_clippy_error(expand_object: &ExpandObject) -> 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();
// let impl_parent = impl_parent(self).into_token_stream();
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
});
}
}
8 changes: 8 additions & 0 deletions derive/src/args/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -188,19 +190,25 @@ fn impl_register(scalar: &Scalar) -> darling::Result<TokenStream> {
})
}

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();
let impl_resolved_own = impl_resolved_own(self).into_token_stream();
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
})
}
}
4 changes: 4 additions & 0 deletions derive/tests/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn test_app_with_generic() {
}

trait GetFoo {
#[allow(dead_code)]
fn get_foo(&self) -> Foo;
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -147,6 +149,7 @@ fn test_app_with_generic_and_lifetime() {
}

trait GetFoo {
#[allow(dead_code)]
fn get_foo(&self) -> Foo;
}

Expand All @@ -158,6 +161,7 @@ fn test_app_with_generic_and_lifetime() {
}
}

#[allow(dead_code)]
struct Other<'a, T>(&'a T)
where
T: GetFoo;
Expand Down
1 change: 1 addition & 0 deletions derive/tests/interface/interface_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit be545a8

Please sign in to comment.