Skip to content

Commit b31bf3a

Browse files
committed
fix: fix remove nested parens for const blocks
1 parent 6b99762 commit b31bf3a

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/expr.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,15 @@ pub(crate) fn format_expr(
175175
// not the `ast::Block` node we're about to rewrite. To prevent dropping inner
176176
// attributes call `rewrite_block` directly.
177177
// See https://github.com/rust-lang/rustfmt/issues/6158
178-
rewrite_block(block, Some(&expr.attrs), opt_label, context, shape)?
178+
rewrite_block_inner(
179+
block,
180+
Some(&expr.attrs),
181+
opt_label,
182+
true,
183+
context,
184+
shape,
185+
false,
186+
)?
179187
}
180188
_ => anon_const.rewrite_result(context, shape)?,
181189
};
@@ -199,6 +207,7 @@ pub(crate) fn format_expr(
199207
false,
200208
context,
201209
shape,
210+
true,
202211
)
203212
}
204213
}
@@ -628,7 +637,7 @@ fn rewrite_block(
628637
context: &RewriteContext<'_>,
629638
shape: Shape,
630639
) -> RewriteResult {
631-
rewrite_block_inner(block, attrs, label, true, context, shape)
640+
rewrite_block_inner(block, attrs, label, true, context, shape, true)
632641
}
633642

634643
fn remove_nested_block(
@@ -638,6 +647,7 @@ fn remove_nested_block(
638647
inner_block: &ast::Block,
639648
context: &RewriteContext<'_>,
640649
shape: Shape,
650+
can_inner_be_removed: bool,
641651
) -> Option<RewriteResult> {
642652
let pre_inner_block_span = mk_sp(block.span.lo(), block_expr.span.lo());
643653
let post_inner_block_span = mk_sp(block_expr.span.hi(), block.span.hi());
@@ -647,12 +657,14 @@ fn remove_nested_block(
647657
if immdiately_contains_comment {
648658
return None;
649659
}
650-
Some(rewrite_block(
660+
Some(rewrite_block_inner(
651661
inner_block,
652662
Some(&block_expr.attrs),
653663
inner_label,
664+
true,
654665
context,
655666
shape,
667+
can_inner_be_removed,
656668
))
657669
}
658670

@@ -663,13 +675,16 @@ fn rewrite_block_inner(
663675
allow_single_line: bool,
664676
context: &RewriteContext<'_>,
665677
shape: Shape,
678+
can_be_removed: bool,
666679
) -> RewriteResult {
667680
debug!("rewrite_block : {:?}", context.snippet(block.span));
668681
let prefix = block_prefix(context, block, shape)?;
669682

670683
let no_attrs = attrs.is_none() || attrs.unwrap().is_empty();
671684

672685
if context.config.remove_nested_blocks()
686+
&& can_be_removed
687+
&& prefix.is_empty()
673688
&& !is_unsafe_block(block)
674689
&& no_attrs
675690
&& label.is_none()
@@ -685,6 +700,7 @@ fn rewrite_block_inner(
685700
inner_block,
686701
context,
687702
shape,
703+
true,
688704
) {
689705
return rw;
690706
}
@@ -701,6 +717,7 @@ fn rewrite_block_inner(
701717
inner_block,
702718
context,
703719
shape,
720+
false,
704721
) {
705722
return Ok(format!("const {}", rw?));
706723
}
@@ -734,7 +751,7 @@ pub(crate) fn rewrite_let_else_block(
734751
context: &RewriteContext<'_>,
735752
shape: Shape,
736753
) -> RewriteResult {
737-
rewrite_block_inner(block, None, None, allow_single_line, context, shape)
754+
rewrite_block_inner(block, None, None, allow_single_line, context, shape, false)
738755
}
739756

740757
// Rewrite condition if the given expression has one.

0 commit comments

Comments
 (0)