@@ -287,13 +287,14 @@ fn rewrite_binary_op(context: &RewriteContext,
287
287
let operator_str = context. codemap . span_to_snippet ( op. span ) . unwrap ( ) ;
288
288
289
289
// 1 = space between lhs expr and operator
290
- let mut result = try_opt ! ( lhs. rewrite( context, width - 1 - operator_str. len( ) , offset) ) ;
290
+ let mut result =
291
+ try_opt ! ( lhs. rewrite( context, context. config. max_width - offset - 1 - operator_str. len( ) , offset) ) ;
291
292
292
293
result. push ( ' ' ) ;
293
294
result. push_str ( & operator_str) ;
294
295
295
296
let remaining_width = match result. rfind ( '\n' ) {
296
- Some ( idx) => ( context . config . max_width + idx) . checked_sub ( result. len ( ) ) . unwrap_or ( 0 ) ,
297
+ Some ( idx) => ( offset + width + idx) . checked_sub ( result. len ( ) ) . unwrap_or ( 0 ) ,
297
298
None => width. checked_sub ( result. len ( ) ) . unwrap_or ( 0 )
298
299
} ;
299
300
@@ -302,7 +303,9 @@ fn rewrite_binary_op(context: &RewriteContext,
302
303
// operations with high precendence close together.
303
304
let rhs_result = try_opt ! ( rhs. rewrite( context, width, offset) ) ;
304
305
305
- if rhs_result. len ( ) > remaining_width {
306
+ // Second condition is needed in case of line break not caused by a
307
+ // shortage of space, but by end-of-line comments, for example.
308
+ if rhs_result. len ( ) > remaining_width || rhs_result. contains ( '\n' ) {
306
309
result. push ( '\n' ) ;
307
310
result. push_str ( & make_indent ( offset) ) ;
308
311
} else {
0 commit comments