Skip to content

Commit 4af1ec5

Browse files
committed
repair break_label format
1 parent 5cd76a3 commit 4af1ec5

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/closures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn rewrite_closure_with_block(
140140
span: body.span,
141141
recovered: false,
142142
};
143-
let block = ::expr::rewrite_block_with_visitor(context, "", &block, None, shape, false)?;
143+
let block = ::expr::rewrite_block_with_visitor(context, "", &block, None, None, shape, false)?;
144144
Some(format!("{} {}", prefix, block))
145145
}
146146

src/expr.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub fn format_expr(
124124
if is_unsafe_block(block) {
125125
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)
126126
} else if let rw @ Some(_) =
127-
rewrite_empty_block(context, block, Some(&expr.attrs), "", shape)
127+
rewrite_empty_block(context, block, Some(&expr.attrs), opt_label, "", shape)
128128
{
129129
// Rewrite block without trying to put it in a single line.
130130
rw
@@ -136,6 +136,7 @@ pub fn format_expr(
136136
&prefix,
137137
block,
138138
Some(&expr.attrs),
139+
opt_label,
139140
shape,
140141
true,
141142
)
@@ -316,9 +317,14 @@ pub fn format_expr(
316317
// satisfy our width restrictions.
317318
ast::ExprKind::InlineAsm(..) => Some(context.snippet(expr.span).to_owned()),
318319
ast::ExprKind::Catch(ref block) => {
319-
if let rw @ Some(_) =
320-
rewrite_single_line_block(context, "do catch ", block, Some(&expr.attrs), shape)
321-
{
320+
if let rw @ Some(_) = rewrite_single_line_block(
321+
context,
322+
"do catch ",
323+
block,
324+
Some(&expr.attrs),
325+
None,
326+
shape,
327+
) {
322328
rw
323329
} else {
324330
// 9 = `do catch `
@@ -543,16 +549,18 @@ fn rewrite_empty_block(
543549
context: &RewriteContext,
544550
block: &ast::Block,
545551
attrs: Option<&[ast::Attribute]>,
552+
label: Option<ast::Label>,
546553
prefix: &str,
547554
shape: Shape,
548555
) -> Option<String> {
556+
let label_str = rewrite_label(label);
549557
if attrs.map_or(false, |a| !inner_attributes(a).is_empty()) {
550558
return None;
551559
}
552560

553561
if block.stmts.is_empty() && !block_contains_comment(block, context.codemap) && shape.width >= 2
554562
{
555-
return Some(format!("{}{{}}", prefix));
563+
return Some(format!("{}{}{{}}", prefix, label_str));
556564
}
557565

558566
// If a block contains only a single-line comment, then leave it on one line.
@@ -565,7 +573,7 @@ fn rewrite_empty_block(
565573
&& !comment_str.starts_with("//")
566574
&& comment_str.len() + 4 <= shape.width
567575
{
568-
return Some(format!("{}{{ {} }}", prefix, comment_str));
576+
return Some(format!("{}{}{{ {} }}", prefix, label_str, comment_str));
569577
}
570578
}
571579

@@ -605,12 +613,14 @@ fn rewrite_single_line_block(
605613
prefix: &str,
606614
block: &ast::Block,
607615
attrs: Option<&[ast::Attribute]>,
616+
label: Option<ast::Label>,
608617
shape: Shape,
609618
) -> Option<String> {
610619
if is_simple_block(block, attrs, context.codemap) {
611620
let expr_shape = shape.offset_left(last_line_width(prefix))?;
612621
let expr_str = block.stmts[0].rewrite(context, expr_shape)?;
613-
let result = format!("{}{{ {} }}", prefix, expr_str);
622+
let label_str = rewrite_label(label);
623+
let result = format!("{}{}{{ {} }}", prefix, label_str, expr_str);
614624
if result.len() <= shape.width && !result.contains('\n') {
615625
return Some(result);
616626
}
@@ -623,10 +633,11 @@ pub fn rewrite_block_with_visitor(
623633
prefix: &str,
624634
block: &ast::Block,
625635
attrs: Option<&[ast::Attribute]>,
636+
label: Option<ast::Label>,
626637
shape: Shape,
627638
has_braces: bool,
628639
) -> Option<String> {
629-
if let rw @ Some(_) = rewrite_empty_block(context, block, attrs, prefix, shape) {
640+
if let rw @ Some(_) = rewrite_empty_block(context, block, attrs, label, prefix, shape) {
630641
return rw;
631642
}
632643

@@ -643,8 +654,9 @@ pub fn rewrite_block_with_visitor(
643654
}
644655

645656
let inner_attrs = attrs.map(inner_attributes);
657+
let label_str = rewrite_label(label);
646658
visitor.visit_block(block, inner_attrs.as_ref().map(|a| &**a), has_braces);
647-
Some(format!("{}{}", prefix, visitor.buffer))
659+
Some(format!("{}{}{}", prefix, label_str, visitor.buffer))
648660
}
649661

650662
impl Rewrite for ast::Block {
@@ -660,20 +672,20 @@ fn rewrite_block(
660672
context: &RewriteContext,
661673
shape: Shape,
662674
) -> Option<String> {
663-
let unsafe_string = block_prefix(context, block, shape)?;
664-
let label_string = rewrite_label(label);
665-
let prefix = format!("{}{}", unsafe_string, label_string);
675+
let prefix = block_prefix(context, block, shape)?;
666676

667677
// shape.width is used only for the single line case: either the empty block `{}`,
668678
// or an unsafe expression `unsafe { e }`.
669-
if let rw @ Some(_) = rewrite_empty_block(context, block, attrs, &prefix, shape) {
679+
if let rw @ Some(_) = rewrite_empty_block(context, block, attrs, label, &prefix, shape) {
670680
return rw;
671681
}
672682

673-
let result = rewrite_block_with_visitor(context, &prefix, block, attrs, shape, true);
683+
let result = rewrite_block_with_visitor(context, &prefix, block, attrs, label, shape, true);
674684
if let Some(ref result_str) = result {
675685
if result_str.lines().count() <= 3 {
676-
if let rw @ Some(_) = rewrite_single_line_block(context, &prefix, block, attrs, shape) {
686+
if let rw @ Some(_) =
687+
rewrite_single_line_block(context, &prefix, block, attrs, label, shape)
688+
{
677689
return rw;
678690
}
679691
}
@@ -1125,7 +1137,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
11251137
let block_str = {
11261138
let old_val = context.is_if_else_block.replace(self.else_block.is_some());
11271139
let result =
1128-
rewrite_block_with_visitor(context, "", self.block, None, block_shape, true);
1140+
rewrite_block_with_visitor(context, "", self.block, None, None, block_shape, true);
11291141
context.is_if_else_block.replace(old_val);
11301142
result?
11311143
};

0 commit comments

Comments
 (0)