Skip to content

Commit

Permalink
Use #[automatically_derived] attribute in each macro (#203)
Browse files Browse the repository at this point in the history
Revealed from
JelteF/derive_more#192 (comment)


## Synopsis

The code, generated by `derive_more`, often becomes a subject of
complains for strict style linters.

## Solution

Use `#[automatically_derived]` attribute on any generated code, so the
code style linters will omit the generated code.
  • Loading branch information
liveseed authored and tyranron committed Oct 5, 2022
1 parent 4ea8166 commit ac02f33
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 34 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
practice.

### New features

- Add support captured identifiers in `Display` derives. So now you can use:
`#[display(fmt = "Prefix: {field}")]` instead of needing to use
`#[display(fmt = "Prefix: {}", field)]`
Expand All @@ -26,7 +27,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Improvements

- Generate doc comments for `Unwrap` and `IsVariant`
- Generate doc comments for `Unwrap` and `IsVariant`.
- Use `#[automatically_derived]` attribute in all macros' expansion for code
style linters to omit the generated code.

### Fixes

Expand Down
4 changes: 2 additions & 2 deletions src/add_assign_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {
};

quote!(
#[automatically_derived]
impl #impl_generics ::core::ops::#trait_ident for #input_type #ty_generics #where_clause {
#[inline]
fn #method_ident(&mut self, rhs: #input_type #ty_generics) {
#(#exprs;
)*
#( #exprs; )*
}
}
)
Expand Down
2 changes: 2 additions & 0 deletions src/add_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {
};

quote!(
#[automatically_derived]
impl #impl_generics ::core::ops::#trait_ident for #input_type #ty_generics #where_clause {
type Output = #output_type;

#[inline]
fn #method_ident(self, rhs: #input_type #ty_generics) -> #output_type {
#block
Expand Down
5 changes: 2 additions & 3 deletions src/as_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let return_types = sub_items.iter().map(|i| &i.4);

Ok(quote! {#(
impl #impl_genericses #trait_paths for #input_type #ty_generics
#where_clauses
{
#[automatically_derived]
impl #impl_genericses #trait_paths for #input_type #ty_generics #where_clauses {
#[inline]
fn as_mut(&mut self) -> &mut #return_types {
#bodies
Expand Down
5 changes: 2 additions & 3 deletions src/as_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let return_types = sub_items.iter().map(|i| &i.4);

Ok(quote! {#(
impl #impl_generics #trait_paths for #input_type #ty_generics
#where_clauses
{
#[automatically_derived]
impl #impl_generics #trait_paths for #input_type #ty_generics #where_clauses {
#[inline]
fn as_ref(&self) -> &#return_types {
#bodies
Expand Down
1 change: 1 addition & 0 deletions src/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn expand(input: &DeriveInput, _: &str) -> TokenStream {
let original_types = &get_field_types(&fields);
quote! {
#[allow(missing_docs)]
#[automatically_derived]
impl #impl_generics #input_type #ty_generics #where_clause {
#[inline]
pub fn new(#(#vars: #original_types),*) -> #input_type #ty_generics {
Expand Down
5 changes: 3 additions & 2 deletions src/deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let (impl_generics, _, where_clause) = generics.split_for_impl();

Ok(quote! {
impl #impl_generics #trait_path for #input_type #ty_generics #where_clause
{
#[automatically_derived]
impl #impl_generics #trait_path for #input_type #ty_generics #where_clause {
type Target = #target;

#[inline]
fn deref(&self) -> &Self::Target {
#body
Expand Down
4 changes: 2 additions & 2 deletions src/deref_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let (impl_generics, _, where_clause) = generics.split_for_impl();

Ok(quote! {
impl #impl_generics #trait_path for #input_type #ty_generics #where_clause
{
#[automatically_derived]
impl #impl_generics #trait_path for #input_type #ty_generics #where_clause {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
#body
Expand Down
5 changes: 2 additions & 3 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ pub fn expand(input: &syn::DeriveInput, trait_name: &str) -> Result<TokenStream>
};

Ok(quote! {
impl #impl_generics #trait_path for #name #ty_generics #where_clause
{
#[allow(unused_variables)]
#[automatically_derived]
impl #impl_generics #trait_path for #name #ty_generics #where_clause {
#[inline]
fn fmt(&self, _derive_more_display_formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
#helper_struct
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub fn expand(
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let render = quote! {
#[automatically_derived]
impl #impl_generics ::std::error::Error for #ident #ty_generics #where_clause {
#source
#provide
Expand Down
5 changes: 3 additions & 2 deletions src/from_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ pub fn struct_from(state: &State, trait_name: &'static str) -> TokenStream {
let body = single_field_data.initializer(&initializers);

quote! {
impl #impl_generics #trait_path for #input_type #ty_generics #where_clause
{
#[automatically_derived]
impl #impl_generics #trait_path for #input_type #ty_generics #where_clause {
type Err = <#field_type as #trait_path>::Err;

#[inline]
fn from_str(src: &str) -> ::core::result::Result<Self, Self::Err> {
Ok(#body)
Expand Down
5 changes: 3 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let (impl_generics, _, where_clause) = new_generics.split_for_impl();
let (_, ty_generics, _) = input.generics.split_for_impl();
Ok(quote! {
impl #impl_generics #trait_path_with_params for #input_type #ty_generics #where_clause
{
#[automatically_derived]
impl #impl_generics #trait_path_with_params for #input_type #ty_generics #where_clause {
type Output = #casted_trait::Output;

#[inline]
fn index(&self, idx: #index_type) -> &Self::Output {
#casted_trait::index(&#member, idx)
Expand Down
4 changes: 2 additions & 2 deletions src/index_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let (impl_generics, _, where_clause) = new_generics.split_for_impl();
let (_, ty_generics, _) = input.generics.split_for_impl();
Ok(quote! {
impl #impl_generics #trait_path_with_params for #input_type #ty_generics #where_clause
{
#[automatically_derived]
impl #impl_generics #trait_path_with_params for #input_type #ty_generics #where_clause {
#[inline]
fn index_mut(&mut self, idx: #index_type) -> &mut Self::Output {
#casted_trait::index_mut(&mut #member, idx)
Expand Down
8 changes: 5 additions & 3 deletions src/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre

(quote! {
#[automatically_derived]
impl #impl_generics ::core::convert::From<#reference_with_lifetime #input_type #ty_generics> for
(#(#into_types),*) #where_clause {

impl #impl_generics
::core::convert::From<#reference_with_lifetime #input_type #ty_generics> for
(#(#into_types),*)
#where_clause
{
#[inline]
fn from(original: #reference_with_lifetime #input_type #ty_generics) -> Self {
(#(#initializers),*)
Expand Down
5 changes: 4 additions & 1 deletion src/into_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let casted_trait =
&quote!(<#reference_with_lifetime #field_type as #trait_path>);
let into_iterator = quote! {
impl #impl_generics #trait_path for #reference_with_lifetime #input_type #ty_generics #where_clause
#[automatically_derived]
impl #impl_generics #trait_path for #reference_with_lifetime #input_type #ty_generics
#where_clause
{
type Item = #casted_trait::Item;
type IntoIter = #casted_trait::IntoIter;

#[inline]
fn into_iter(self) -> Self::IntoIter {
#casted_trait::into_iter(#reference #member)
Expand Down
3 changes: 2 additions & 1 deletion src/is_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
}

let imp = quote! {
impl #imp_generics #enum_name #type_generics #where_clause{
#[automatically_derived]
impl #imp_generics #enum_name #type_generics #where_clause {
#(#funcs)*
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/mul_assign_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let (impl_generics, _, where_clause) = generics.split_for_impl();

Ok(quote!(
impl #impl_generics #trait_path<#scalar_ident> for #input_type #ty_generics #where_clause{
#[automatically_derived]
impl #impl_generics #trait_path<#scalar_ident> for #input_type #ty_generics #where_clause {
#[inline]
fn #method_ident(&mut self, rhs: #scalar_ident) {
#(#exprs;
)*
#( #exprs; )*
}
}
))
Expand Down
2 changes: 2 additions & 0 deletions src/mul_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let body = multi_field_data.initializer(&initializers);
let (impl_generics, _, where_clause) = generics.split_for_impl();
Ok(quote!(
#[automatically_derived]
impl #impl_generics #trait_path_with_params for #input_type #ty_generics #where_clause {
type Output = #input_type #ty_generics;

#[inline]
fn #method_ident(self, rhs: #scalar_ident) -> #input_type #ty_generics {
#body
Expand Down
2 changes: 2 additions & 0 deletions src/not_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ pub fn expand(input: &DeriveInput, trait_name: &str) -> TokenStream {
};

quote!(
#[automatically_derived]
impl #impl_generics ::core::ops::#trait_ident for #input_type #ty_generics #where_clause {
type Output = #output_type;

#[inline]
fn #method_ident(self) -> #output_type {
#block
Expand Down
1 change: 1 addition & 0 deletions src/sum_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
let identity = multi_field_data.initializer(&initializers);

Ok(quote!(
#[automatically_derived]
impl #impl_generics #trait_path for #input_type #ty_generics #where_clause {
#[inline]
fn #method_ident<I: ::core::iter::Iterator<Item = Self>>(iter: I) -> Self {
Expand Down
9 changes: 6 additions & 3 deletions src/try_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,14 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
};

let try_from = quote! {
impl #impl_generics ::core::convert::TryFrom<#reference_with_lifetime #input_type #ty_generics> for
(#(#reference_with_lifetime #original_types),*) #where_clause {
#[automatically_derived]
impl #impl_generics
::core::convert::TryFrom<#reference_with_lifetime #input_type #ty_generics> for
(#(#reference_with_lifetime #original_types),*)
#where_clause
{
type Error = &'static str;

#[allow(unused_variables)]
#[inline]
fn try_from(value: #reference_with_lifetime #input_type #ty_generics) -> ::core::result::Result<Self, Self::Error> {
match value {
Expand Down
3 changes: 2 additions & 1 deletion src/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
}

let imp = quote! {
impl #imp_generics #enum_name #type_generics #where_clause{
#[automatically_derived]
impl #imp_generics #enum_name #type_generics #where_clause {
#(#funcs)*
}
};
Expand Down

0 comments on commit ac02f33

Please sign in to comment.