Skip to content

Commit

Permalink
feat: wrap await expr into block instead of paren
Browse files Browse the repository at this point in the history
  • Loading branch information
fMeow committed Oct 10, 2020
1 parent 3ddb5ca commit 5c4232a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
//! #[maybe_async::test(
//! feature="is_sync",
//! async(all(not(feature="is_sync"), feature="async_std"), async_std::test),
//! async(all(not(feature="is_sync"), feature="tokio"), tokio::test)
//! )]
//! async(all(not(feature="is_sync"), feature="tokio"), tokio::test)
//! )]
//! async fn test_async_fn() {
//! let res = async_fn().await;
//! assert_eq!(res, true);
Expand Down Expand Up @@ -334,7 +334,7 @@ fn convert_async(input: &mut Item, send: bool) -> TokenStream2 {
}

fn convert_sync(input: &mut Item) -> TokenStream2 {
let token = match input {
match input {
Item::Impl(item) => {
for inner in &mut item.items {
if let ImplItem::Method(ref mut method) = inner {
Expand All @@ -361,8 +361,8 @@ fn convert_sync(input: &mut Item) -> TokenStream2 {
}
AsyncAwaitRemoval.remove_async_await(quote!(#item))
}
};
quote!(#[allow(unused_parens)]#token).into()
}
.into()
}

/// maybe_async attribute macro
Expand Down
23 changes: 14 additions & 9 deletions src/visit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
parse_quote, token,
parse_quote,
visit_mut::{self, VisitMut},
Expr, ExprBlock, ExprParen, File,
Attribute, Block, Expr, ExprBlock, File,
};

pub struct AsyncAwaitRemoval;
Expand All @@ -19,19 +19,24 @@ impl VisitMut for AsyncAwaitRemoval {
fn visit_expr_mut(&mut self, node: &mut Expr) {
match node {
Expr::Await(expr) => {
let mut attrs = expr.attrs.clone();
let allow: Attribute = parse_quote! {#[allow(unused_braces)]};
attrs.push(allow);

let inner = &expr.base;
let block = ExprParen {
attrs: expr.attrs.clone(),
expr: parse_quote!(#inner),
paren_token: token::Paren(expr.await_token.span),
};
*node = Expr::Paren(block)
let block: Block = parse_quote!({#inner});

*node = Expr::Block(ExprBlock {
attrs,
block,
label: None,
});
}
Expr::Async(expr) => {
let inner = &expr.block;
let block = ExprBlock {
attrs: expr.attrs.clone(),
block: parse_quote!(#inner),
block: inner.clone(),
label: None,
};
*node = Expr::Block(block);
Expand Down

0 comments on commit 5c4232a

Please sign in to comment.