Skip to content

Commit 64768cf

Browse files
committed
fix label prefix
1 parent 150765d commit 64768cf

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/expr.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,28 @@ pub fn format_expr(
122122
match expr_type {
123123
ExprType::Statement => {
124124
if is_unsafe_block(block) {
125-
rewrite_block(block, Some(&expr.attrs), context, shape)
125+
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
126126
} else if let rw @ Some(_) =
127127
rewrite_empty_block(context, block, Some(&expr.attrs), "", shape)
128128
{
129129
// Rewrite block without trying to put it in a single line.
130130
rw
131131
} else {
132132
let prefix = block_prefix(context, block, shape)?;
133-
let label_string = rewrite_label(opt_label);
134133

135134
rewrite_block_with_visitor(
136135
context,
137-
&format!("{}{}", &prefix, &label_string),
136+
&prefix,
138137
block,
139138
Some(&expr.attrs),
140139
shape,
141140
true,
142141
)
143142
}
144143
}
145-
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+
}
146147
}
147148
}
148149
ast::ExprKind::Match(ref cond, ref arms) => {
@@ -328,6 +329,7 @@ pub fn format_expr(
328329
rewrite_block(
329330
block,
330331
Some(&expr.attrs),
332+
None,
331333
context,
332334
Shape::legacy(budget, shape.indent)
333335
)?
@@ -645,17 +647,20 @@ pub fn rewrite_block_with_visitor(
645647

646648
impl Rewrite for ast::Block {
647649
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
648-
rewrite_block(self, None, context, shape)
650+
rewrite_block(self, None, None, context, shape)
649651
}
650652
}
651653

652654
fn rewrite_block(
653655
block: &ast::Block,
654656
attrs: Option<&[ast::Attribute]>,
657+
label: Option<ast::Label>,
655658
context: &RewriteContext,
656659
shape: Shape,
657660
) -> Option<String> {
658-
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);
659664

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

tests/source/label_break.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ fn main() {
33

44
'empty_block: {}
55

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+
618
let result = 'block: {
719
if foo() {
820
// comment
9-
break 'block 1;
21+
break 'block 1;
1022
}
1123
if bar() { /* comment */
1224
break 'block 2;

tests/target/label_break.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22
fn main() {
33
{}
44

5-
let result = {
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: {
618
if foo() {
719
// comment
820
break 'block 1;

0 commit comments

Comments
 (0)