diff --git a/src/config.rs b/src/config.rs index 295c1076b5f..c02d618437f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -307,4 +307,5 @@ create_config! { take_source_hints: bool, true, "Retain some formatting characteristics from the source code"; hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment"; wrap_comments: bool, false, "Break comments to fit on the line"; + wrap_match_arms: bool, true, "Wrap multiline match arms in blocks"; } diff --git a/src/expr.rs b/src/expr.rs index a0d7e08878f..29d6d32c0dd 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -941,10 +941,11 @@ impl Rewrite for ast::Arm { } let body = match **body { - ast::Expr { node: ast::ExprBlock(ref b), .. } if !is_unsafe_block(b) && - is_simple_block(b, - context.codemap) => { - b.expr.as_ref().map(|e| &**e).unwrap() + ast::Expr { node: ast::ExprBlock(ref block), .. } if !is_unsafe_block(block) && + is_simple_block(block, + context.codemap) && + context.config.wrap_match_arms => { + block.expr.as_ref().map(|e| &**e).unwrap() } ref x => x, }; @@ -959,7 +960,8 @@ impl Rewrite for ast::Arm { let rewrite = nop_block_collapse(body.rewrite(context, budget, offset), budget); match rewrite { - Some(ref body_str) if !body_str.contains('\n') || comma.is_empty() => { + Some(ref body_str) if !body_str.contains('\n') || !context.config.wrap_match_arms || + comma.is_empty() => { return Some(format!("{}{} => {}{}", attr_str.trim_left(), pats_str, @@ -970,7 +972,7 @@ impl Rewrite for ast::Arm { } } - // FIXME: we're doing a second rewrite of the expr -- this may not be + // FIXME: we're doing a second rewrite of the expr; This may not be // necessary. let body_budget = try_opt!(width.checked_sub(context.config.tab_spaces)); let indent = context.block_indent.block_indent(context.config); @@ -980,13 +982,20 @@ impl Rewrite for ast::Arm { indent), body_budget)); let indent_str = offset.block_indent(context.config).to_string(context.config); + let (body_prefix, body_suffix) = if context.config.wrap_match_arms { + (" {", "}") + } else { + ("", "") + }; - Some(format!("{}{} => {{\n{}{}\n{}}}", + Some(format!("{}{} =>{}\n{}{}\n{}{}", attr_str.trim_left(), pats_str, + body_prefix, indent_str, next_line_body, - offset.to_string(context.config))) + offset.to_string(context.config), + body_suffix)) } } diff --git a/tests/source/match-nowrap.rs b/tests/source/match-nowrap.rs new file mode 100644 index 00000000000..83e0fbbd450 --- /dev/null +++ b/tests/source/match-nowrap.rs @@ -0,0 +1,12 @@ +// rustfmt-wrap_match_arms: false +// Match expressions, no unwrapping of block arms or wrapping of multiline +// expressions. + +fn foo() { + match x { + a => { foo() } + b => + (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb), + } +} diff --git a/tests/target/match-nowrap.rs b/tests/target/match-nowrap.rs new file mode 100644 index 00000000000..db2a874c838 --- /dev/null +++ b/tests/target/match-nowrap.rs @@ -0,0 +1,13 @@ +// rustfmt-wrap_match_arms: false +// Match expressions, no unwrapping of block arms or wrapping of multiline +// expressions. + +fn foo() { + match x { + a => { + foo() + } + b => (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb), + } +}