Skip to content

Commit 1b3257d

Browse files
authored
Merge pull request #2726 from csmoe/label_break
Format label break
2 parents 632fab4 + 64768cf commit 1b3257d

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

src/expr.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,19 @@ pub fn format_expr(
118118
| ast::ExprKind::While(..)
119119
| ast::ExprKind::WhileLet(..) => to_control_flow(expr, expr_type)
120120
.and_then(|control_flow| control_flow.rewrite(context, shape)),
121-
// FIXME(topecongiro): Handle label on a block (#2722).
122-
ast::ExprKind::Block(ref block, _) => {
121+
ast::ExprKind::Block(ref block, opt_label) => {
123122
match expr_type {
124123
ExprType::Statement => {
125124
if is_unsafe_block(block) {
126-
rewrite_block(block, Some(&expr.attrs), context, shape)
125+
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
127126
} else if let rw @ Some(_) =
128127
rewrite_empty_block(context, block, Some(&expr.attrs), "", shape)
129128
{
130129
// Rewrite block without trying to put it in a single line.
131130
rw
132131
} else {
133132
let prefix = block_prefix(context, block, shape)?;
133+
134134
rewrite_block_with_visitor(
135135
context,
136136
&prefix,
@@ -141,7 +141,9 @@ pub fn format_expr(
141141
)
142142
}
143143
}
144-
ExprType::SubExpression => rewrite_block(block, Some(&expr.attrs), context, shape),
144+
ExprType::SubExpression => {
145+
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
146+
}
145147
}
146148
}
147149
ast::ExprKind::Match(ref cond, ref arms) => {
@@ -327,6 +329,7 @@ pub fn format_expr(
327329
rewrite_block(
328330
block,
329331
Some(&expr.attrs),
332+
None,
330333
context,
331334
Shape::legacy(budget, shape.indent)
332335
)?
@@ -644,17 +647,20 @@ pub fn rewrite_block_with_visitor(
644647

645648
impl Rewrite for ast::Block {
646649
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
647-
rewrite_block(self, None, context, shape)
650+
rewrite_block(self, None, None, context, shape)
648651
}
649652
}
650653

651654
fn rewrite_block(
652655
block: &ast::Block,
653656
attrs: Option<&[ast::Attribute]>,
657+
label: Option<ast::Label>,
654658
context: &RewriteContext,
655659
shape: Shape,
656660
) -> Option<String> {
657-
let prefix = block_prefix(context, block, shape)?;
661+
let unsafe_string = block_prefix(context, block, shape)?;
662+
let label_string = rewrite_label(label);
663+
let prefix = format!("{}{}", unsafe_string, label_string);
658664

659665
// shape.width is used only for the single line case: either the empty block `{}`,
660666
// or an unsafe expression `unsafe { e }`.

tests/source/label_break.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// format with label break value.
2+
fn main() {
3+
4+
'empty_block: {}
5+
6+
'block: {
7+
do_thing();
8+
if condition_not_met() {
9+
break 'block;
10+
}
11+
do_next_thing();
12+
if condition_not_met() {
13+
break 'block;
14+
}
15+
do_last_thing();
16+
}
17+
18+
let result = 'block: {
19+
if foo() {
20+
// comment
21+
break 'block 1;
22+
}
23+
if bar() { /* comment */
24+
break 'block 2;
25+
}
26+
3
27+
};
28+
}

tests/target/label_break.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// format with label break value.
2+
fn main() {
3+
{}
4+
5+
{
6+
do_thing();
7+
if condition_not_met() {
8+
break 'block;
9+
}
10+
do_next_thing();
11+
if condition_not_met() {
12+
break 'block;
13+
}
14+
do_last_thing();
15+
}
16+
17+
let result = 'block: {
18+
if foo() {
19+
// comment
20+
break 'block 1;
21+
}
22+
if bar() {
23+
/* comment */
24+
break 'block 2;
25+
}
26+
3
27+
};
28+
}

0 commit comments

Comments
 (0)