Skip to content

Commit

Permalink
Wrap let-else expression in paren if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 12, 2024
1 parent c5fe61a commit ebcad93
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
9 changes: 0 additions & 9 deletions src/classify.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ebcad93

Please sign in to comment.