Skip to content

Commit e5a017e

Browse files
committed
Wrap let-else expression in paren if needed
1 parent c5fe61a commit e5a017e

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/classify.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
use crate::expr::Expr;
2-
#[cfg(feature = "parsing")]
32
use crate::generics::TypeParamBound;
4-
#[cfg(feature = "parsing")]
53
use crate::path::{Path, PathArguments};
6-
#[cfg(feature = "parsing")]
74
use crate::punctuated::Punctuated;
8-
#[cfg(feature = "parsing")]
95
use crate::ty::{ReturnType, Type};
10-
#[cfg(feature = "parsing")]
116
use proc_macro2::{Delimiter, TokenStream, TokenTree};
12-
#[cfg(feature = "parsing")]
137
use std::ops::ControlFlow;
148

159
pub(crate) fn requires_terminator(expr: &Expr) -> bool {
@@ -57,7 +51,6 @@ pub(crate) fn requires_terminator(expr: &Expr) -> bool {
5751
}
5852

5953
/// Whether the expression's last token is `}`.
60-
#[cfg(feature = "parsing")]
6154
pub(crate) fn expr_trailing_brace(mut expr: &Expr) -> bool {
6255
loop {
6356
match expr {
@@ -116,7 +109,6 @@ pub(crate) fn expr_trailing_brace(mut expr: &Expr) -> bool {
116109
}
117110
}
118111

119-
#[cfg(feature = "parsing")]
120112
fn type_trailing_brace(mut ty: &Type) -> bool {
121113
fn last_type_in_path(path: &Path) -> Option<&Type> {
122114
match &path.segments.last().unwrap().arguments {
@@ -174,7 +166,6 @@ fn type_trailing_brace(mut ty: &Type) -> bool {
174166
}
175167
}
176168

177-
#[cfg(feature = "parsing")]
178169
fn tokens_trailing_brace(tokens: &TokenStream) -> bool {
179170
if let Some(TokenTree::Group(last)) = tokens.clone().into_iter().last() {
180171
last.delimiter() == Delimiter::Brace

src/stmt.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,10 @@ pub(crate) mod parsing {
411411

412412
#[cfg(feature = "printing")]
413413
mod printing {
414+
use crate::classify;
414415
use crate::expr;
415416
use crate::stmt::{Block, Local, Stmt, StmtMacro};
417+
use crate::token;
416418
use proc_macro2::TokenStream;
417419
use quote::{ToTokens, TokenStreamExt};
418420

@@ -448,7 +450,11 @@ mod printing {
448450
self.pat.to_tokens(tokens);
449451
if let Some(init) = &self.init {
450452
init.eq_token.to_tokens(tokens);
451-
init.expr.to_tokens(tokens);
453+
if init.diverge.is_some() && classify::expr_trailing_brace(&init.expr) {
454+
token::Paren::default().surround(tokens, |tokens| init.expr.to_tokens(tokens));
455+
} else {
456+
init.expr.to_tokens(tokens);
457+
}
452458
if let Some((else_token, diverge)) = &init.diverge {
453459
else_token.to_tokens(tokens);
454460
diverge.to_tokens(tokens);

0 commit comments

Comments
 (0)