diff --git a/src/classify.rs b/src/classify.rs index 609fe8f4d9..44995fb66a 100644 --- a/src/classify.rs +++ b/src/classify.rs @@ -1,15 +1,9 @@ use crate::expr::Expr; -#[cfg(feature = "parsing")] use crate::generics::TypeParamBound; -#[cfg(feature = "parsing")] use crate::path::{Path, PathArguments}; -#[cfg(feature = "parsing")] use crate::punctuated::Punctuated; -#[cfg(feature = "parsing")] use crate::ty::{ReturnType, Type}; -#[cfg(feature = "parsing")] use proc_macro2::{Delimiter, TokenStream, TokenTree}; -#[cfg(feature = "parsing")] use std::ops::ControlFlow; pub(crate) fn requires_terminator(expr: &Expr) -> bool { @@ -57,7 +51,6 @@ pub(crate) fn requires_terminator(expr: &Expr) -> bool { } /// Whether the expression's last token is `}`. -#[cfg(feature = "parsing")] pub(crate) fn expr_trailing_brace(mut expr: &Expr) -> bool { loop { match expr { @@ -116,7 +109,6 @@ pub(crate) fn expr_trailing_brace(mut expr: &Expr) -> bool { } } -#[cfg(feature = "parsing")] fn type_trailing_brace(mut ty: &Type) -> bool { fn last_type_in_path(path: &Path) -> Option<&Type> { match &path.segments.last().unwrap().arguments { @@ -174,7 +166,6 @@ fn type_trailing_brace(mut ty: &Type) -> bool { } } -#[cfg(feature = "parsing")] fn tokens_trailing_brace(tokens: &TokenStream) -> bool { if let Some(TokenTree::Group(last)) = tokens.clone().into_iter().last() { last.delimiter() == Delimiter::Brace diff --git a/src/mac.rs b/src/mac.rs index b3e6b41efe..4f10e0b881 100644 --- a/src/mac.rs +++ b/src/mac.rs @@ -41,7 +41,7 @@ impl MacroDelimiter { } } - #[cfg(all(feature = "full", feature = "parsing"))] + #[cfg(all(feature = "full", any(feature = "parsing", feature = "printing")))] pub(crate) fn is_brace(&self) -> bool { match self { MacroDelimiter::Brace(_) => true, diff --git a/src/stmt.rs b/src/stmt.rs index 49a5ad3e10..87e8715200 100644 --- a/src/stmt.rs +++ b/src/stmt.rs @@ -411,8 +411,10 @@ pub(crate) mod parsing { #[cfg(feature = "printing")] mod printing { + use crate::classify; use crate::expr; use crate::stmt::{Block, Local, Stmt, StmtMacro}; + use crate::token; use proc_macro2::TokenStream; use quote::{ToTokens, TokenStreamExt}; @@ -448,7 +450,11 @@ mod printing { self.pat.to_tokens(tokens); if let Some(init) = &self.init { init.eq_token.to_tokens(tokens); - init.expr.to_tokens(tokens); + if init.diverge.is_some() && classify::expr_trailing_brace(&init.expr) { + token::Paren::default().surround(tokens, |tokens| init.expr.to_tokens(tokens)); + } else { + init.expr.to_tokens(tokens); + } if let Some((else_token, diverge)) = &init.diverge { else_token.to_tokens(tokens); diverge.to_tokens(tokens);