-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Feat/format conditions multi line #12636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Feat/format conditions multi line #12636
Conversation
|
grandizzy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, makes sense IMO (pending @0xrusowsky ). Please update the readme too to reflect the new config https://github.com/foundry-rs/foundry/tree/master/crates/fmt#configuration and add an ui_test as in https://github.com/foundry-rs/foundry/tree/master/crates/fmt#testing
|
the impl LGTM, but imo it would be nice to make the output prettier. if possible, let's aim for: if (
newNumber % 2 == 0
|| newNumber % 2 == 1
|| newNumber != 0
|| newNumber != 1
|| newNumber != 2
) {i think you should be able to achieve if with an extra box when the multiline config is enabled |
| // Normalize trailing newlines: ensure exactly one \n at the end | ||
| // This must happen AFTER scan_eof() to catch any newlines added during EOF-flush | ||
| // If there's no newline at all, add one | ||
| if !self.out.ends_with('\n') { | ||
| self.out.push('\n'); | ||
| } else { | ||
| // If there are multiple newlines, remove extras to leave exactly one | ||
| while self.out.ends_with("\n\n") { | ||
| self.out.pop(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to introduce this logic?
Motivation
Closes #12508
When conditions in
ifstatements are formatted inline (all on one line), coverage analysis tools like lcov cannot properly distinguish which individual conditions within the chain are actually covered by tests. This happens because the tool sees only one line with the condition, making it difficult to determine branch coverage for each condition.For example:
With inline formatting, if a test only covers the first condition (e.g.,
newNumber % 2 == 0), lcov may show the entire line as covered, but it cannot indicate which specific conditions were actually tested. This makes it difficult to identify untested conditions and achieve accurate branch coverage analysis.Solution
Added a new
format_conditionsconfiguration option infoundry.tomlthat allows placing each condition on a separate line:When
format_conditions = "multi", logical operators (||,&&) inifconditions are formatted with each condition on a separate line:PR Checklist