Skip to content

Format label break #2726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ 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)
{
// Rewrite block without trying to put it in a single line.
rw
} else {
let prefix = block_prefix(context, block, shape)?;

rewrite_block_with_visitor(
context,
&prefix,
Expand All @@ -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) => {
Expand Down Expand Up @@ -327,6 +329,7 @@ pub fn format_expr(
rewrite_block(
block,
Some(&expr.attrs),
None,
context,
Shape::legacy(budget, shape.indent)
)?
Expand Down Expand Up @@ -644,17 +647,20 @@ pub fn rewrite_block_with_visitor(

impl Rewrite for ast::Block {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
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<ast::Label>,
context: &RewriteContext,
shape: Shape,
) -> Option<String> {
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 }`.
Expand Down
28 changes: 28 additions & 0 deletions tests/source/label_break.rs
Original file line number Diff line number Diff line change
@@ -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
};
}
28 changes: 28 additions & 0 deletions tests/target/label_break.rs
Original file line number Diff line number Diff line change
@@ -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
};
}