diff --git a/src/expr.rs b/src/expr.rs index d9524c46fa5..19b7f8b734e 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1701,25 +1701,20 @@ impl Rewrite for ast::Arm { }; let pats_str = try_opt!(write_list(&items, &fmt)); - let guard_shape = if pats_str.contains('\n') { - shape.with_max_width(context.config) - } else { - shape - }; - let guard_str = try_opt!(rewrite_guard( context, guard, - guard_shape, + shape, trimmed_last_line_width(&pats_str), )); + let pats_len = pats_str.len(); let pats_str = format!("{}{}", pats_str, guard_str); let (mut extend, body) = match body.node { ast::ExprKind::Block(ref block) if !is_unsafe_block(block) && is_simple_block(block, context.codemap) && - context.config.wrap_match_arms() => { + context.config.wrap_match_arms() => { if let ast::StmtKind::Expr(ref expr) = block.stmts[0].node { (false, &**expr) } else { @@ -1761,11 +1756,15 @@ impl Rewrite for ast::Arm { match rewrite { Some(ref body_str) if (!body_str.contains('\n') && body_str.len() <= arm_shape.width) || - !context.config.wrap_match_arms() || - (extend && first_line_width(body_str) <= arm_shape.width) || - is_block => { + !context.config.wrap_match_arms() || + (extend && first_line_width(body_str) <= arm_shape.width) || + is_block => + { let block_sep = match context.config.control_brace_style() { ControlBraceStyle::AlwaysNextLine if is_block => alt_block_sep.as_str(), + _ if guard.is_some() && pats_str.contains('\n') && is_block && + body_str != "{}" && + pats_len > context.config.tab_spaces() => alt_block_sep.as_str(), _ => " ", }; @@ -1848,33 +1847,30 @@ fn rewrite_guard( // First try to fit the guard string on the same line as the pattern. // 4 = ` if `, 5 = ` => {` if let Some(cond_shape) = shape - .shrink_left(pattern_width + 4) + .offset_left(pattern_width + 4) .and_then(|s| s.sub_width(5)) { if let Some(cond_str) = guard .rewrite(context, cond_shape) .and_then(|s| s.rewrite(context, cond_shape)) { - if !cond_str.contains('\n') { + if !cond_str.contains('\n') || pattern_width <= context.config.tab_spaces() { return Some(format!(" if {}", cond_str)); } } } // Not enough space to put the guard after the pattern, try a newline. - // 3 == `if ` - if let Some(cond_shape) = Shape::indented( - shape.indent.block_indent(context.config) + 3, - context.config, - ).sub_width(3) + // 3 = `if `, 5 = ` => {` + if let Some(cond_shape) = + Shape::indented(shape.indent.block_indent(context.config), context.config) + .offset_left(3) + .and_then(|s| s.sub_width(5)) { if let Some(cond_str) = guard.rewrite(context, cond_shape) { return Some(format!( "\n{}if {}", - shape - .indent - .block_indent(context.config) - .to_string(context.config), + cond_shape.indent.to_string(context.config), cond_str )); } @@ -1920,7 +1916,6 @@ fn rewrite_pat_expr( // The expression won't fit on the current line, jump to next. let nested_shape = shape - .block() .block_indent(context.config.tab_spaces()) .with_max_width(context.config); let nested_indent_str = nested_shape.indent.to_string(context.config); diff --git a/src/types.rs b/src/types.rs index b617c433f10..6fd070291af 100644 --- a/src/types.rs +++ b/src/types.rs @@ -210,7 +210,8 @@ fn rewrite_segment( match **params { ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() || !data.types.is_empty() || - !data.bindings.is_empty() => { + !data.bindings.is_empty() => + { let param_list = data.lifetimes .iter() .map(SegmentParam::LifeTime) diff --git a/tests/source/long-match-arms-brace-newline.rs b/tests/source/long-match-arms-brace-newline.rs index 927ada0ffb2..bc5c9deef68 100644 --- a/tests/source/long-match-arms-brace-newline.rs +++ b/tests/source/long-match-arms-brace-newline.rs @@ -1,4 +1,5 @@ // rustfmt-format_strings: true +// rustfmt-force_format_strings: true // rustfmt-max_width: 80 // rustfmt-control_brace_style: AlwaysNextLine diff --git a/tests/target/configs-control_style-rfc.rs b/tests/target/configs-control_style-rfc.rs index 20742da2195..417c33c59f8 100644 --- a/tests/target/configs-control_style-rfc.rs +++ b/tests/target/configs-control_style-rfc.rs @@ -28,9 +28,10 @@ fn issue1656() { match rewrite { Some(ref body_str) if (!body_str.contains('\n') && body_str.len() <= arm_shape.width) || - !context.config.wrap_match_arms() || - (extend && first_line_width(body_str) <= arm_shape.width) || - is_block => { + !context.config.wrap_match_arms() || + (extend && first_line_width(body_str) <= arm_shape.width) || + is_block => + { return None; } _ => {} diff --git a/tests/target/long-match-arms-brace-newline.rs b/tests/target/long-match-arms-brace-newline.rs index 6f4e3a8de21..2a956e5b4d0 100644 --- a/tests/target/long-match-arms-brace-newline.rs +++ b/tests/target/long-match-arms-brace-newline.rs @@ -1,4 +1,5 @@ // rustfmt-format_strings: true +// rustfmt-force_format_strings: true // rustfmt-max_width: 80 // rustfmt-control_brace_style: AlwaysNextLine diff --git a/tests/target/match.rs b/tests/target/match.rs index bd6376f5153..f08c5acc9be 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -324,8 +324,8 @@ fn guards() { if foooooooooooooo && barrrrrrrrrrrr => {} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if fooooooooooooooooooooo && - (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || - cccccccccccccccccccccccccccccccccccccccc) => {} + (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || + cccccccccccccccccccccccccccccccccccccccc) => {} } }