diff --git a/src/expr.rs b/src/expr.rs index 8961cec4240..ea3ac5f58f2 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -118,12 +118,11 @@ pub fn format_expr( | ast::ExprKind::While(..) | ast::ExprKind::WhileLet(..) => to_control_flow(expr, expr_type) .and_then(|control_flow| control_flow.rewrite(context, shape)), - // FIXME(topecongiro): Handle label on a block (#2722). - ast::ExprKind::Block(ref block, _) => { + ast::ExprKind::Block(ref block, opt_label) => { match expr_type { ExprType::Statement => { if is_unsafe_block(block) { - rewrite_block(block, Some(&expr.attrs), context, shape) + rewrite_block(block, Some(&expr.attrs), opt_label, context, shape) } else if let rw @ Some(_) = rewrite_empty_block(context, block, Some(&expr.attrs), "", shape) { @@ -131,6 +130,7 @@ pub fn format_expr( rw } else { let prefix = block_prefix(context, block, shape)?; + rewrite_block_with_visitor( context, &prefix, @@ -141,7 +141,9 @@ pub fn format_expr( ) } } - ExprType::SubExpression => rewrite_block(block, Some(&expr.attrs), context, shape), + ExprType::SubExpression => { + rewrite_block(block, Some(&expr.attrs), opt_label, context, shape) + } } } ast::ExprKind::Match(ref cond, ref arms) => { @@ -327,6 +329,7 @@ pub fn format_expr( rewrite_block( block, Some(&expr.attrs), + None, context, Shape::legacy(budget, shape.indent) )? @@ -644,17 +647,20 @@ pub fn rewrite_block_with_visitor( impl Rewrite for ast::Block { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - rewrite_block(self, None, context, shape) + rewrite_block(self, None, None, context, shape) } } fn rewrite_block( block: &ast::Block, attrs: Option<&[ast::Attribute]>, + label: Option, context: &RewriteContext, shape: Shape, ) -> Option { - let prefix = block_prefix(context, block, shape)?; + let unsafe_string = block_prefix(context, block, shape)?; + let label_string = rewrite_label(label); + let prefix = format!("{}{}", unsafe_string, label_string); // shape.width is used only for the single line case: either the empty block `{}`, // or an unsafe expression `unsafe { e }`. diff --git a/tests/source/label_break.rs b/tests/source/label_break.rs new file mode 100644 index 00000000000..2c79fd35e70 --- /dev/null +++ b/tests/source/label_break.rs @@ -0,0 +1,28 @@ +// format with label break value. +fn main() { + +'empty_block: {} + +'block: { + do_thing(); + if condition_not_met() { + break 'block; + } + do_next_thing(); + if condition_not_met() { + break 'block; + } + do_last_thing(); +} + +let result = 'block: { + if foo() { + // comment + break 'block 1; + } + if bar() { /* comment */ + break 'block 2; + } + 3 +}; +} \ No newline at end of file diff --git a/tests/target/label_break.rs b/tests/target/label_break.rs new file mode 100644 index 00000000000..afbd8e6fcdf --- /dev/null +++ b/tests/target/label_break.rs @@ -0,0 +1,28 @@ +// format with label break value. +fn main() { + {} + + { + do_thing(); + if condition_not_met() { + break 'block; + } + do_next_thing(); + if condition_not_met() { + break 'block; + } + do_last_thing(); + } + + let result = 'block: { + if foo() { + // comment + break 'block 1; + } + if bar() { + /* comment */ + break 'block 2; + } + 3 + }; +}