@@ -835,24 +835,48 @@ impl<'ast> State<'_, 'ast> {
835835 self . s . offset ( self . ind ) ;
836836 self . print_expr ( rhs) ;
837837 }
838- ast:: ExprKind :: Binary ( _, op, _) => {
839- // Binary expressions: check if we need to break and indent
840- if force_break || self . estimate_lhs_size ( rhs, op) + lhs_size > space_left {
841- if !self . is_bol_or_only_ind ( ) {
838+ ast:: ExprKind :: Binary ( lhs, op, _) => {
839+ let print_inline = |this : & mut Self | {
840+ this. print_sep ( Separator :: Nbsp ) ;
841+ this. neverbreak ( ) ;
842+ this. print_expr ( rhs) ;
843+ } ;
844+ let print_with_break = |this : & mut Self , force_break : bool | {
845+ if !this. is_bol_or_only_ind ( ) {
842846 if force_break {
843- self . print_sep ( Separator :: Hardbreak ) ;
847+ this . print_sep ( Separator :: Hardbreak ) ;
844848 } else {
845- self . print_sep ( Separator :: Space ) ;
849+ this . print_sep ( Separator :: Space ) ;
846850 }
847851 }
848- self . s . offset ( self . ind ) ;
849- self . s . ibox ( self . ind ) ;
850- self . print_expr ( rhs) ;
851- self . end ( ) ;
852- } else {
853- self . print_sep ( Separator :: Nbsp ) ;
854- self . neverbreak ( ) ;
855- self . print_expr ( rhs) ;
852+ this. s . offset ( this. ind ) ;
853+ this. s . ibox ( this. ind ) ;
854+ this. print_expr ( rhs) ;
855+ this. end ( ) ;
856+ } ;
857+
858+ // Binary expressions: check if we need to break and indent
859+ if force_break {
860+ print_with_break ( self , true ) ;
861+ } else if self . estimate_lhs_size ( rhs, op) + lhs_size > space_left {
862+ if has_complex_successor ( & rhs. kind , true )
863+ && get_callee_head_size ( lhs) + lhs_size <= space_left
864+ {
865+ // Keep complex exprs (where callee fits) inline, as they will have breaks
866+ if matches ! ( lhs. kind, ast:: ExprKind :: Call ( ..) ) {
867+ self . s . ibox ( -self . ind ) ;
868+ print_inline ( self ) ;
869+ self . end ( ) ;
870+ } else {
871+ print_inline ( self ) ;
872+ }
873+ } else {
874+ print_with_break ( self , false ) ;
875+ }
876+ }
877+ // Otherwise, if expr fits, ensure no breaks
878+ else {
879+ print_inline ( self ) ;
856880 }
857881 }
858882 _ => {
@@ -2926,6 +2950,7 @@ pub(super) fn get_callee_head_size(callee: &ast::Expr<'_>) -> usize {
29262950 _ => member_ident. as_str ( ) . len ( ) ,
29272951 }
29282952 }
2953+ ast:: ExprKind :: Binary ( lhs, _, _) => get_callee_head_size ( lhs) ,
29292954
29302955 // If the callee is not an identifier or member access, it has no "head"
29312956 _ => 0 ,
0 commit comments