@@ -19,7 +19,7 @@ use syntax::{ast, ptr};
19
19
use comment:: { combine_strs_with_missing_comments, rewrite_comment} ;
20
20
use config:: { Config , ControlBraceStyle , IndentStyle } ;
21
21
use expr:: {
22
- format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line,
22
+ format_expr, is_empty_block, is_simple_block, is_unsafe_block, prefer_next_line, rewrite_cond ,
23
23
rewrite_multiple_patterns, ExprType , RhsTactics ,
24
24
} ;
25
25
use lists:: { itemize_list, write_list, ListFormatting } ;
@@ -231,7 +231,7 @@ fn rewrite_match_arm(
231
231
) -> Option < String > {
232
232
let ( missing_span, attrs_str) = if !arm. attrs . is_empty ( ) {
233
233
if contains_skip ( & arm. attrs ) {
234
- let ( _, body) = flatten_arm_body ( context, & arm. body ) ;
234
+ let ( _, body) = flatten_arm_body ( context, & arm. body , None ) ;
235
235
// `arm.span()` does not include trailing comma, add it manually.
236
236
return Some ( format ! (
237
237
"{}{}" ,
@@ -313,16 +313,27 @@ fn block_can_be_flattened<'a>(
313
313
// (extend, body)
314
314
// @extend: true if the arm body can be put next to `=>`
315
315
// @body: flattened body, if the body is block with a single expression
316
- fn flatten_arm_body < ' a > ( context : & ' a RewriteContext , body : & ' a ast:: Expr ) -> ( bool , & ' a ast:: Expr ) {
316
+ fn flatten_arm_body < ' a > (
317
+ context : & ' a RewriteContext ,
318
+ body : & ' a ast:: Expr ,
319
+ opt_shape : Option < Shape > ,
320
+ ) -> ( bool , & ' a ast:: Expr ) {
317
321
let can_extend =
318
322
|expr| !context. config . force_multiline_blocks ( ) && can_flatten_block_around_this ( expr) ;
319
323
320
324
if let Some ( ref block) = block_can_be_flattened ( context, body) {
321
325
if let ast:: StmtKind :: Expr ( ref expr) = block. stmts [ 0 ] . node {
322
326
if let ast:: ExprKind :: Block ( ..) = expr. node {
323
- flatten_arm_body ( context, expr)
327
+ flatten_arm_body ( context, expr, None )
324
328
} else {
325
- ( can_extend ( expr) , & * expr)
329
+ let cond_becomes_muti_line = opt_shape
330
+ . and_then ( |shape| rewrite_cond ( context, expr, shape) )
331
+ . map_or ( false , |cond| cond. contains ( '\n' ) ) ;
332
+ if cond_becomes_muti_line {
333
+ ( false , & * body)
334
+ } else {
335
+ ( can_extend ( expr) , & * expr)
336
+ }
326
337
}
327
338
} else {
328
339
( false , & * body)
@@ -341,7 +352,11 @@ fn rewrite_match_body(
341
352
arrow_span : Span ,
342
353
is_last : bool ,
343
354
) -> Option < String > {
344
- let ( extend, body) = flatten_arm_body ( context, body) ;
355
+ let ( extend, body) = flatten_arm_body (
356
+ context,
357
+ body,
358
+ shape. offset_left ( extra_offset ( pats_str, shape) + 4 ) ,
359
+ ) ;
345
360
let ( is_block, is_empty_block) = if let ast:: ExprKind :: Block ( ref block, _) = body. node {
346
361
(
347
362
true ,
0 commit comments