@@ -154,6 +154,11 @@ fn check_ancestry(ancestor: &SyntaxNode, descendent: &SyntaxNode) -> bool {
154154 bail ( )
155155}
156156
157+ fn next_token_of ( node : & SyntaxNode ) -> Option < ast:: SyntaxToken > {
158+ let last = node. last_token ( ) ?;
159+ skip_trivia_token ( last. next_token ( ) ?, Direction :: Next )
160+ }
161+
157162impl Expr {
158163 pub fn precedence ( & self ) -> ExprPrecedence {
159164 precedence ( self )
@@ -197,6 +202,8 @@ impl Expr {
197202 if is_parent_call_expr && is_field_expr {
198203 return true ;
199204 }
205+ let place_of_parent =
206+ || place_of. ancestors ( ) . find ( |it| it. parent ( ) . is_none_or ( |p| & p == parent. syntax ( ) ) ) ;
200207
201208 // Special-case block weirdness
202209 if parent. child_is_followed_by_a_block ( ) {
@@ -226,15 +233,24 @@ impl Expr {
226233 // For `&&`, we avoid introducing `<ret-like> && <expr>` into a binary chain.
227234
228235 if self . precedence ( ) == ExprPrecedence :: Jump
229- && let Some ( node) =
230- place_of. ancestors ( ) . find ( |it| it. parent ( ) . is_none_or ( |p| & p == parent. syntax ( ) ) )
231- && let Some ( next) =
232- node. last_token ( ) . and_then ( |t| skip_trivia_token ( t. next_token ( ) ?, Direction :: Next ) )
236+ && let Some ( node) = place_of_parent ( )
237+ && let Some ( next) = next_token_of ( & node)
233238 && matches ! ( next. kind( ) , T ![ ||] | T ![ &&] )
234239 {
235240 return true ;
236241 }
237242
243+ // Special-case `2 as x < 3`
244+ if let ast:: Expr :: CastExpr ( it) = self
245+ && let Some ( ty) = it. ty ( )
246+ && ty. syntax ( ) . last_token ( ) . and_then ( |it| ast:: NameLike :: cast ( it. parent ( ) ?) ) . is_some ( )
247+ && let Some ( node) = place_of_parent ( )
248+ && let Some ( next) = next_token_of ( & node)
249+ && matches ! ( next. kind( ) , T ![ <] | T ![ <<] )
250+ {
251+ return true ;
252+ }
253+
238254 if self . is_paren_like ( )
239255 || parent. is_paren_like ( )
240256 || self . is_prefix ( )
0 commit comments