From 100d9164f2b2dbfb5ffcafc19fd925f55626acc0 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Fri, 13 Dec 2024 14:06:02 -0800 Subject: [PATCH] Avoid associating #[from] with lint allow --- impl/src/expand.rs | 16 +++++++++++----- tests/test_lints.rs | 11 +++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/impl/src/expand.rs b/impl/src/expand.rs index 4441a24..c89985f 100644 --- a/impl/src/expand.rs +++ b/impl/src/expand.rs @@ -169,15 +169,18 @@ fn impl_struct(input: Struct) -> TokenStream { let from = unoptional_type(from_field.ty); let source_var = Ident::new("source", span); let body = from_initializer(from_field, backtrace_field, &source_var); - quote_spanned! {span=> - #[allow(deprecated, unused_qualifications, clippy::needless_lifetimes)] + let impl_impl = quote_spanned! {span=> #[automatically_derived] impl #impl_generics ::core::convert::From<#from> for #ty #ty_generics #where_clause { fn from(#source_var: #from) -> Self { #ty #body } } - } + }; + Some(quote! { + #[allow(deprecated, unused_qualifications, clippy::needless_lifetimes)] + #impl_impl + }) }); if input.generics.type_params().next().is_some() { @@ -433,14 +436,17 @@ fn impl_enum(input: Enum) -> TokenStream { let from = unoptional_type(from_field.ty); let source_var = Ident::new("source", span); let body = from_initializer(from_field, backtrace_field, &source_var); - Some(quote_spanned! {span=> - #[allow(deprecated, unused_qualifications, clippy::needless_lifetimes)] + let impl_impl = quote_spanned! {span=> #[automatically_derived] impl #impl_generics ::core::convert::From<#from> for #ty #ty_generics #where_clause { fn from(#source_var: #from) -> Self { #ty::#variant #body } } + }; + Some(quote! { + #[allow(deprecated, unused_qualifications, clippy::needless_lifetimes)] + #impl_impl }) }); diff --git a/tests/test_lints.rs b/tests/test_lints.rs index d272454..af5830d 100644 --- a/tests/test_lints.rs +++ b/tests/test_lints.rs @@ -4,6 +4,17 @@ use thiserror::Error; pub use std::error::Error; +#[test] +fn test_allow_attributes() { + #![deny(clippy::allow_attributes)] + + #[derive(Error, Debug)] + #[error("...")] + pub struct MyError(#[from] anyhow::Error); + + let _: MyError; +} + #[test] fn test_unused_qualifications() { #![deny(unused_qualifications)]