@@ -5,7 +5,7 @@ use stdx::always;
55use crate :: {
66 AstNode , Direction , SyntaxNode , T ,
77 algo:: skip_trivia_token,
8- ast:: { self , BinaryOp , Expr , HasArgList , RangeItem } ,
8+ ast:: { self , BinExpr , BinaryOp , Expr , HasArgList , RangeItem } ,
99 match_ast,
1010} ;
1111
@@ -221,14 +221,24 @@ impl Expr {
221221 return false ;
222222 }
223223
224- // Keep parens when `(return ...)` is followed by `||`;
225- // otherwise it would become `return || <closure>` with different semantics
226- if let Expr :: ReturnExpr ( _) = self
227- && let Some ( paren_expr) = self . syntax ( ) . parent ( ) . and_then ( ast:: ParenExpr :: cast)
224+ // Keep parens when a ret-like expr is followed by `||` or `&&`.
225+ // For `||`, removing parens could reparse as `<ret-like> || <closure>`.
226+ // For `&&`, we avoid introducing `<ret-like> && <expr>` into a binary chain.
227+
228+ if matches ! (
229+ self ,
230+ Expr :: ReturnExpr ( _)
231+ | Expr :: BreakExpr ( _)
232+ | Expr :: BecomeExpr ( _)
233+ | Expr :: YeetExpr ( _)
234+ | Expr :: YieldExpr ( _)
235+ ) && let Some ( paren_expr) = self . syntax ( ) . parent ( ) . and_then ( ast:: ParenExpr :: cast)
236+ && let Some ( parent_expr) = paren_expr. syntax ( ) . parent ( ) . and_then ( ast:: Expr :: cast)
237+ && BinExpr :: can_cast ( parent_expr. syntax ( ) . kind ( ) )
228238 && let Some ( r_paren) = paren_expr. r_paren_token ( )
229239 && let Some ( next) =
230240 r_paren. next_token ( ) . and_then ( |t| skip_trivia_token ( t, Direction :: Next ) )
231- && next. kind ( ) == T ! [ ||]
241+ && matches ! ( next. kind( ) , T ![ ||] | T ! [ && ] )
232242 {
233243 return true ;
234244 }
0 commit comments