From b3fdf43365715b6dc85b1e911699ab26696c3aba Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 8 Mar 2021 12:13:24 -0800 Subject: [PATCH 1/3] Add regression test for issue 154 --- tests/test.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test.rs b/tests/test.rs index 6f74bee..604d092 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1300,3 +1300,25 @@ pub mod issue152 { async fn f(&self) -> Self::Assoc {} } } + +// https://github.com/dtolnay/async-trait/issues/154 +pub mod issue154 { + #![deny(clippy::items_after_statements)] + + use async_trait::async_trait; + + #[async_trait] + pub trait MyTrait { + async fn f(&self); + } + + pub struct Struct; + + #[async_trait] + impl MyTrait for Struct { + async fn f(&self) { + const MAX: u16 = 128; + println!("{}", MAX); + } + } +} From 61676412a224d4e4784980eb1ed9c26153cffcd1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 8 Mar 2021 12:17:07 -0800 Subject: [PATCH 2/3] Avoid items_after_statements lint in generated code --- src/expand.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/expand.rs b/src/expand.rs index 6cc6ef0..be47d22 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -357,19 +357,24 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) { } let stmts = &block.stmts; + let stmts = if decls.is_empty() { + quote!(#(#stmts)*) + } else { + quote!({ #(#stmts)* }) + }; let let_ret = match &mut sig.output { ReturnType::Default => quote_spanned! {block.brace_token.span=> - let _: () = { #(#decls)* #(#stmts)* }; + let _: () = { #(#decls)* #stmts }; }, ReturnType::Type(_, ret) => { if contains_associated_type_impl_trait(context, ret) { - quote!(#(#decls)* #(#stmts)*) + quote!(#(#decls)* #stmts) } else { quote_spanned! {block.brace_token.span=> if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> { return __ret; } - let __ret: #ret = { #(#decls)* #(#stmts)* }; + let __ret: #ret = { #(#decls)* #stmts }; #[allow(unreachable_code)] __ret } From a7a47966f004e6258e404ec3f743f67d49f374cb Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 8 Mar 2021 12:19:06 -0800 Subject: [PATCH 3/3] Reduce nesting of items_after_statements workaround --- src/expand.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/expand.rs b/src/expand.rs index be47d22..e78c6c4 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -357,24 +357,25 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) { } let stmts = &block.stmts; - let stmts = if decls.is_empty() { - quote!(#(#stmts)*) - } else { - quote!({ #(#stmts)* }) - }; let let_ret = match &mut sig.output { ReturnType::Default => quote_spanned! {block.brace_token.span=> - let _: () = { #(#decls)* #stmts }; + #(#decls)* + let _: () = { #(#stmts)* }; }, ReturnType::Type(_, ret) => { if contains_associated_type_impl_trait(context, ret) { - quote!(#(#decls)* #stmts) + if decls.is_empty() { + quote!(#(#stmts)*) + } else { + quote!(#(#decls)* { #(#stmts)* }) + } } else { quote_spanned! {block.brace_token.span=> if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> { return __ret; } - let __ret: #ret = { #(#decls)* #stmts }; + #(#decls)* + let __ret: #ret = { #(#stmts)* }; #[allow(unreachable_code)] __ret }