Skip to content

Commit b6d0c06

Browse files
authored
fix(fmt): rustfmt-like blank lines in stmt blocks (#11914)
1 parent e546834 commit b6d0c06

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

crates/fmt/src/state/common.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -529,18 +529,21 @@ impl<'ast> State<'_, 'ast> {
529529
BlockFormat::NoBraces(Some(offset)) => {
530530
let enabled =
531531
!self.inline_config.is_disabled(Span::new(block_lo, block_lo + BytePos(1)))
532-
&& !self.handle_span(self.cursor.span(block_lo), false);
533-
match self.peek_comment_before(block_lo).map(|cmnt| (cmnt.span, cmnt.style)) {
532+
&& !self.handle_span(self.cursor.span(block_lo), true);
533+
match self.peek_comment().and_then(|cmnt| {
534+
if cmnt.span.hi() < block_lo { Some((cmnt.span, cmnt.style)) } else { None }
535+
}) {
534536
Some((span, style)) => {
535537
if enabled {
536538
// Inline config is not disabled and span not handled
537539
if !self.inline_config.is_disabled(span) || style.is_isolated() {
538540
self.cursor.advance_to(span.lo(), true);
539541
self.break_offset(SIZE_INFINITY as usize, offset);
540542
}
541-
if let Some(cmnt) = self
542-
.print_comments(block_lo, CommentConfig::default().offset(offset))
543-
&& !cmnt.is_mixed()
543+
if let Some(cmnt) = self.print_comments(
544+
block_lo,
545+
CommentConfig::skip_leading_ws(false).offset(offset),
546+
) && !cmnt.is_mixed()
544547
&& !cmnt.is_blank()
545548
{
546549
self.s.offset(offset);

crates/fmt/testdata/Repros/fmt.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ function noBlanksLinesBeforeIdentifiers() public {
182182
timelockController.grantRole(keccak256("EXECUTOR_ROLE"), address(0));
183183
}
184184

185+
// https://github.com/foundry-rs/foundry/issues/11913
186+
function rustfmtBlankLinesInStmtBlocks() public {
187+
if (someCondition) {
188+
bar = true;
189+
190+
emit Foo(bar);
191+
}
192+
}
193+
185194
contract NestedCallsTest is Test {
186195
string constant errMsg = "User provided message";
187196
uint256 constant maxDecimals = 77;

crates/fmt/testdata/Repros/original.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ function noBlanksLinesBeforeIdentifiers() public {
187187
.grantRole(keccak256("EXECUTOR_ROLE"), address(0));
188188
}
189189

190+
// https://github.com/foundry-rs/foundry/issues/11913
191+
function rustfmtBlankLinesInStmtBlocks() public {
192+
if (someCondition) {
193+
194+
195+
196+
bar = true;
197+
198+
199+
200+
emit Foo(bar);
201+
202+
203+
}
204+
}
205+
190206
contract NestedCallsTest is Test {
191207
string constant errMsg = "User provided message";
192208
uint256 constant maxDecimals = 77;

crates/fmt/testdata/Repros/sorted.fmt.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ function noBlanksLinesBeforeIdentifiers() public {
183183
timelockController.grantRole(keccak256("EXECUTOR_ROLE"), address(0));
184184
}
185185

186+
// https://github.com/foundry-rs/foundry/issues/11913
187+
function rustfmtBlankLinesInStmtBlocks() public {
188+
if (someCondition) {
189+
bar = true;
190+
191+
emit Foo(bar);
192+
}
193+
}
194+
186195
contract NestedCallsTest is Test {
187196
string constant errMsg = "User provided message";
188197
uint256 constant maxDecimals = 77;

crates/fmt/testdata/Repros/tab.fmt.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ function noBlanksLinesBeforeIdentifiers() public {
183183
timelockController.grantRole(keccak256("EXECUTOR_ROLE"), address(0));
184184
}
185185

186+
// https://github.com/foundry-rs/foundry/issues/11913
187+
function rustfmtBlankLinesInStmtBlocks() public {
188+
if (someCondition) {
189+
bar = true;
190+
191+
emit Foo(bar);
192+
}
193+
}
194+
186195
contract NestedCallsTest is Test {
187196
string constant errMsg = "User provided message";
188197
uint256 constant maxDecimals = 77;

0 commit comments

Comments
 (0)