Skip to content

Preserve conditional control flow when adding braces - #382

Merged
thomhurst merged 1 commit into
mainfrom
fix/conditional-brace-insertion
Nov 20, 2026
Merged

thomhurst merged 1 commit into
mainfrom
fix/conditional-brace-insertion

Conversation

@thomhurst

Copy link
Copy Markdown
Collaborator

Curb skips brace insertion when a conditional directive can separate the braces or change which statement an if controls. This keeps both configurations in the reported example buildable.

Prompt summary: Investigate the brace insertion failure reported in #380. Fix the behavior, add regression tests, and open a pull request.

Why

A shared body can carry #endif as leading trivia. Curb emits the opening brace before that trivia and the closing brace after the body. The default round-trip parse sees both braces, while another symbol configuration can see only the closing brace.

What

Conditional headers and bodies

A brace candidate is skipped when a conditional directive falls between the start of its header and the end of its body. This covers shared bodies and headers whose closing tokens serve a different construct in another branch.

Inactive else clauses

Curb leaves an embedded body unwrapped when a trailing inactive branch could attach an else to an unmatched if inside it. The inner if can still gain braces around its own body.

Regression coverage

The tests cover both symbol settings for the reported example, deterministic reflow, a header shared with an invocation, and an inactive else. A control case checks brace insertion for an entire if within one branch.

Verify

dotnet run --project tests/Nullean.Curb.Tests -c Release -- --treenode-filter '/*/*/PreferBraces*/*'

All 20 brace cases passed, and all six new expectations survived dotnet format whitespace. The CLI reproduction builds under both symbol settings, and the inactive else example produces 2 with ALT defined.

Measured against the same 2,124-file generated C# corpus, both versions rewrote 2,124 files in each mode. The fix reduces the net line additions by 8 with width disabled and 10 with reflow. Output differences are confined to the new regression snippets.

Fixes #380

The default parse can see both added braces even when another symbol
set sees only one. Preserve conditional headers and bodies, and keep
inactive else clauses attached to their original if.
@thomhurst thomhurst added the bug A defect fixed (patch) label Nov 20, 2026
@thomhurst
thomhurst enabled auto-merge (squash) November 20, 2026 16:21
@thomhurst
thomhurst merged commit 89d5d25 into main Nov 20, 2026
12 checks passed
@thomhurst
thomhurst deleted the fix/conditional-brace-insertion branch November 20, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A defect fixed (patch)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

curb format unbalances braces when preprocessor branches share an if body

1 participant