Skip to content

fix(fmt): dropped statements for single-line **nested** if statements#13560

Open
quangloc99 wants to merge 4 commits intofoundry-rs:masterfrom
quangloc99:fix-fmt-single-line-block
Open

fix(fmt): dropped statements for single-line **nested** if statements#13560
quangloc99 wants to merge 4 commits intofoundry-rs:masterfrom
quangloc99:fix-fmt-single-line-block

Conversation

@quangloc99
Copy link
Copy Markdown
Contributor

@quangloc99 quangloc99 commented Feb 28, 2026

Motivation

Originally I attempted to fix #13555 (now fix #13675 ). After it is closes with #13566, to be frank I was still a bit skeptical with the solution. After some testing, the following case fails:

// before forge fmt
if (condition) if (condition) { execute(); execute(); }


// after forge fmt
if (condition) if (condition) execute();
execute();

A more realistic case for this is while (cond) if (cond) { ... }.

More tests can be seen in the commit daf10d2

Solution

The solution presented in this PR is to change is_multiline_block_stmt to check the deeper structure of the statement. But besides the missing checks, I think the root cause is the caching of single_line_stmt: this variable is updated only at the top level of when looking at the if statement, and only the top if statement is examined carefully, while the nested if is only consider a normal statement. So imo my fix might not be enough.

I understand that caching here is necessary to make sure the work is done once. But to fix this root cause, a more reliable way should be calculating this variable for all nodes of the tree from bottom-up instead of top-down like this.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@quangloc99 quangloc99 force-pushed the fix-fmt-single-line-block branch from 262f86d to e690477 Compare March 4, 2026 16:02
@quangloc99 quangloc99 changed the title fix(fmt): print_state_as_block multi-statements inline checking fix(fmt): dropped statements for single-line **nested** if statements Mar 4, 2026
@zerosnacks
Copy link
Copy Markdown
Member

cc @DaniPopes / @0xrusowsky when time permits

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

Labels

None yet

Projects

Status: No status

2 participants