Unverified Commit 3a552fb8 authored by Keith Yeung's avatar Keith Yeung Committed by GitHub
Browse files

Allow lint attributes to be passed through to the impl block (#272)

parent e71d9c1a
Pipeline #142104 passed with stages
in 22 minutes and 14 seconds
......@@ -28,6 +28,8 @@ use proc_macro_crate::{crate_name, FoundCrate};
use syn::spanned::Spanned;
use syn::{Data, Field, Fields, DeriveInput, Error};
use crate::utils::is_lint_attribute;
mod decode;
mod encode;
mod utils;
......@@ -51,8 +53,9 @@ fn include_parity_scale_codec_crate() -> proc_macro2::TokenStream {
}
/// Wraps the impl block in a "dummy const"
fn wrap_with_dummy_const(impl_block: proc_macro2::TokenStream) -> proc_macro::TokenStream {
fn wrap_with_dummy_const(input: DeriveInput, impl_block: proc_macro2::TokenStream) -> proc_macro::TokenStream {
let parity_codec_crate = include_parity_scale_codec_crate();
let attrs = input.attrs.into_iter().filter(is_lint_attribute);
let generated = quote! {
const _: () = {
......@@ -60,6 +63,7 @@ fn wrap_with_dummy_const(impl_block: proc_macro2::TokenStream) -> proc_macro::To
#[cfg_attr(feature = "cargo-clippy", allow(useless_attribute))]
#[allow(rust_2018_idioms)]
#parity_codec_crate
#(#attrs)*
#impl_block
};
};
......@@ -169,7 +173,7 @@ pub fn encode_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream
impl #impl_generics _parity_scale_codec::EncodeLike for #name #ty_generics #where_clause {}
};
wrap_with_dummy_const(impl_block)
wrap_with_dummy_const(input, impl_block)
}
/// Derive `parity_scale_codec::Decode` and for struct and enum.
......@@ -216,7 +220,7 @@ pub fn decode_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream
}
};
wrap_with_dummy_const(impl_block)
wrap_with_dummy_const(input, impl_block)
}
/// Derive `parity_scale_codec::Compact` and `parity_scale_codec::CompactAs` for struct with single
......@@ -327,5 +331,5 @@ pub fn compact_as_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStr
}
};
wrap_with_dummy_const(impl_block)
wrap_with_dummy_const(input, impl_block)
}
......@@ -221,6 +221,14 @@ pub fn check_attributes(input: &DeriveInput) -> syn::Result<()> {
Ok(())
}
// Check if the attribute is `#[allow(..)]`, `#[deny(..)]`, `#[forbid(..)]` or `#[warn(..)]`.
pub fn is_lint_attribute(attr: &Attribute) -> bool {
attr.path.is_ident("allow")
|| attr.path.is_ident("deny")
|| attr.path.is_ident("forbid")
|| attr.path.is_ident("warn")
}
// Ensure a field is decorated only with the following attributes:
// * `#[codec(skip)]`
// * `#[codec(compact)]`
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment