From 29f33e13bbb9bf76790b5c89e41a4f712fe4d98f Mon Sep 17 00:00:00 2001 From: toasteater <48371905+toasteater@users.noreply.github.com> Date: Thu, 15 Apr 2021 11:43:07 +0000 Subject: [PATCH] Add automatically_derived and allow lints in macros This should prevent clippy lints from showing up in user code in the future. --- gdnative-core/src/nativescript/macros.rs | 1 + gdnative-derive/src/lib.rs | 15 +++++++++++++++ gdnative-derive/src/methods.rs | 3 ++- gdnative-derive/src/native_script.rs | 4 ++++ gdnative-derive/src/varargs.rs | 5 ++++- gdnative-derive/src/variant/from.rs | 4 +++- gdnative-derive/src/variant/to.rs | 3 ++- 7 files changed, 31 insertions(+), 4 deletions(-) diff --git a/gdnative-core/src/nativescript/macros.rs b/gdnative-core/src/nativescript/macros.rs index b190f2fb8..43a191f14 100644 --- a/gdnative-core/src/nativescript/macros.rs +++ b/gdnative-core/src/nativescript/macros.rs @@ -101,6 +101,7 @@ macro_rules! godot_wrap_method_inner { use ::gdnative::FromVarargs; #[derive(FromVarargs)] + #[allow(clippy::used_underscore_binding)] struct Args { $($pname: $pty,)* $(#[opt] $opt_pname: $opt_pty,)* diff --git a/gdnative-derive/src/lib.rs b/gdnative-derive/src/lib.rs index cdb0444f3..1de146e7f 100644 --- a/gdnative-derive/src/lib.rs +++ b/gdnative-derive/src/lib.rs @@ -238,3 +238,18 @@ pub fn derive_from_varargs(input: TokenStream) -> TokenStream { Err(err) => err.to_compile_error().into(), } } + +/// Returns a standard header for derived implementations. +/// +/// Adds the `automatically_derived` attribute and prevents common lints from triggering +/// in user code. See: +/// +/// - https://doc.rust-lang.org/reference/attributes/derive.html +/// - https://doc.rust-lang.org/rustc/lints/groups.html +/// - https://github.com/rust-lang/rust-clippy#clippy +fn automatically_derived() -> proc_macro2::TokenStream { + quote! { + #[automatically_derived] + #[allow(nonstandard_style, unused, clippy::style, clippy::complexity, clippy::perf, clippy::pedantic)] + } +} diff --git a/gdnative-derive/src/methods.rs b/gdnative-derive/src/methods.rs index 6edbe9f53..e26979e57 100644 --- a/gdnative-derive/src/methods.rs +++ b/gdnative-derive/src/methods.rs @@ -68,6 +68,7 @@ pub(crate) struct ExportArgs { } pub(crate) fn derive_methods(item_impl: ItemImpl) -> TokenStream2 { + let derived = crate::automatically_derived(); let (impl_block, export) = impl_gdnative_expose(item_impl); let class_name = export.class_ty; @@ -140,9 +141,9 @@ pub(crate) fn derive_methods(item_impl: ItemImpl) -> TokenStream2 { .collect::>(); quote::quote!( - #impl_block + #derived impl gdnative::nativescript::NativeClassMethods for #class_name { fn register(#builder: &::gdnative::nativescript::init::ClassBuilder) { use gdnative::nativescript::init::*; diff --git a/gdnative-derive/src/native_script.rs b/gdnative-derive/src/native_script.rs index a063e44d0..411c69040 100644 --- a/gdnative-derive/src/native_script.rs +++ b/gdnative-derive/src/native_script.rs @@ -18,9 +18,11 @@ pub(crate) struct DeriveData { } pub(crate) fn impl_empty_nativeclass(derive_input: &DeriveInput) -> TokenStream2 { + let derived = crate::automatically_derived(); let name = &derive_input.ident; quote! { + #derived impl ::gdnative::prelude::NativeClass for #name { type Base = ::gdnative::api::Object; type UserData = ::gdnative::prelude::LocalCellData; @@ -36,6 +38,7 @@ pub(crate) fn impl_empty_nativeclass(derive_input: &DeriveInput) -> TokenStream2 } pub(crate) fn derive_native_class(derive_input: &DeriveInput) -> Result { + let derived = crate::automatically_derived(); let data = parse_derive_input(&derive_input)?; // generate NativeClass impl @@ -117,6 +120,7 @@ pub(crate) fn derive_native_class(derive_input: &DeriveInput) -> Result Result { + let derived = crate::automatically_derived(); + if let Data::Struct(struct_data) = input.data { let ident = input.ident; @@ -32,6 +34,7 @@ pub(crate) fn derive_from_varargs(input: DeriveInput) -> Result &fields.unnamed, Fields::Unit => { return Ok(quote! { + #derived impl #generics ::gdnative::nativescript::init::method::FromVarargs for #ident #generics #where_clause { fn read<'a>( #input_ident: &mut ::gdnative::nativescript::init::method::Varargs<'a>, @@ -109,7 +112,7 @@ pub(crate) fn derive_from_varargs(input: DeriveInput) -> Result>(); Ok(quote! { - #[allow(unused_variables)] + #derived impl #generics ::gdnative::nativescript::init::method::FromVarargs for #ident #generics #where_clause { fn read<'a>( #input_ident: &mut ::gdnative::nativescript::init::method::Varargs<'a>, diff --git a/gdnative-derive/src/variant/from.rs b/gdnative-derive/src/variant/from.rs index 2cdfef401..d304556c9 100644 --- a/gdnative-derive/src/variant/from.rs +++ b/gdnative-derive/src/variant/from.rs @@ -12,6 +12,8 @@ pub(crate) fn expand_from_variant(derive_data: DeriveData) -> Result Result ::gdnative::core_types::Variant { use #trait_path;