Skip to content

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

Open
Dnreikronos wants to merge 3 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 3 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
Low risk: changes are limited to formatting behavior for a specific if shape (empty then-block with else/else if) and add regression tests; no runtime/compiler semantics are affected.

Overview
Fixes forc-fmt output so an empty if body followed by else/else if no longer collapses onto a single line by emitting a newline (except during inline width-probe formatting).

Adds regression coverage for this case (including a long-condition else variant) in both expression-level and integration formatter tests; includes a small reformat-only adjustment in sway-lib-std (raw_ptr.write) to match the corrected style.

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

forc-fmt issue on raw_ptr::write.

1 participant