Skip to content

Commit 1fcf234

Browse files
committed
fix label prefix
1 parent 150765d commit 1fcf234

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/expr.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,26 @@ 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 => rewrite_block(block, Some(&expr.attrs), opt_label, context, shape),
146145
}
147146
}
148147
ast::ExprKind::Match(ref cond, ref arms) => {
@@ -328,6 +327,7 @@ pub fn format_expr(
328327
rewrite_block(
329328
block,
330329
Some(&expr.attrs),
330+
None,
331331
context,
332332
Shape::legacy(budget, shape.indent)
333333
)?
@@ -645,17 +645,20 @@ pub fn rewrite_block_with_visitor(
645645

646646
impl Rewrite for ast::Block {
647647
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
648-
rewrite_block(self, None, context, shape)
648+
rewrite_block(self, None, None, context, shape)
649649
}
650650
}
651651

652652
fn rewrite_block(
653653
block: &ast::Block,
654654
attrs: Option<&[ast::Attribute]>,
655+
label: Option<ast::Label>,
655656
context: &RewriteContext,
656657
shape: Shape,
657658
) -> Option<String> {
658-
let prefix = block_prefix(context, block, shape)?;
659+
let unsafe_string = block_prefix(context, block, shape)?;
660+
let label_string = rewrite_label(label);
661+
let prefix = format!("{}{}", unsafe_string, label_string);
659662

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

tests/source/label_break.rs

+13-1
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

+13-1
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)