Skip to content

Fix forc-fmt empty if-body + else collapsing onto one line#7628

Open
Dnreikronos wants to merge 4 commits into
FuelLabs:masterfrom
Dnreikronos:Dnreikronos/fix-forc-fmt-empty-if-else-collapse
Open

Fix forc-fmt empty if-body + else collapsing onto one line#7628
Dnreikronos wants to merge 4 commits into
FuelLabs:masterfrom
Dnreikronos:Dnreikronos/fix-forc-fmt-empty-if-else-collapse

Conversation

@Dnreikronos
Copy link
Copy Markdown
Contributor

Description

Fixes #7625

forc-fmt collapses empty if-body + else onto a single line:

// Before (wrong):
if __size_of::<T>() == 0 {    } else if __is_reference_type::<T>() {

// After (correct):
if __size_of::<T>() == 0 {
} else if __is_reference_type::<T>() {

Root cause: In format_then_block, when the then-block is empty and has an else clause, block_unindent() was called but no newline was written — so the closing } from format_else_opt ended up on the same line.

Fix: Write a newline after the empty body when an else clause exists, and skip block_unindent since close_curly_brace in format_else_opt already handles unindentation. Guarded with LineStyle::Inline to avoid injecting newlines during width measurement probes.

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

When an if-block has an empty body and an else clause, the formatter
wrote no newline before the closing brace, producing collapsed output
like `if cond {    } else if ...` on a single line.

Write a newline after the empty body when an else clause exists, and
skip block_unindent since close_curly_brace in format_else_opt already
handles it. Guard with LineStyle::Inline to avoid injecting newlines
during width measurement probes.

Closes FuelLabs#7625
Add expr-level test for empty if + else-if and integration tests for
empty if + else-if and empty if + else with long lines that prevent
inlining.
@Dnreikronos Dnreikronos requested review from a team as code owners May 23, 2026 18:38
@cursor
Copy link
Copy Markdown

cursor Bot commented May 23, 2026

PR Summary

Low Risk
Scoped formatter behavior change with tests; stdlib change is formatting-only.

Overview
forc-fmt no longer glues an empty if body and the following else / else if onto one line (e.g. if cond { } else if …). In format_then_block, when the then-block is empty and an else is present, the formatter now emits a newline before the closing brace instead of only calling block_unindent, except during LineStyle::Inline width probes so inline layout logic stays unchanged.

Regression coverage adds an expression-level fmt test and integration tests for empty-then with else if and with a plain else. sway-lib-std raw_ptr::write is reformatted to the corrected style (multi-line empty zero-size branch).

Reviewed by Cursor Bugbot for commit 12b35b7. Bugbot is set up for automated code reviews on this repo. Configure here.

@Dnreikronos
Copy link
Copy Markdown
Contributor Author

bugbot run

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 12b35b7. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

forc-fmt issue on raw_ptr::write.

1 participant