Skip to content

fix: don't end an unbraced if body at a semicolon in a nested header - #1

Open
captainurist wants to merge 1 commit into
developfrom
nested-control-body-braces
Open

captainurist wants to merge 1 commit into
developfrom
nested-control-body-braces

Conversation

@captainurist

Copy link
Copy Markdown
Member

The readability/braces check for unbraced if and else bodies takes the first semicolon after the condition as the end of the body. With if (foo) over for (int i = 0; i < n; ++i), that semicolon is inside the for header, so cpplint reports "If/else bodies with multiple statements require braces" for a single statement. The same happens for an if with an init-statement.

The check now steps over the header of a nested if, for, while or switch before looking for the semicolon. Two statements under such a header are still reported. New cases in testConditionals cover both. On OpenEnroth, linting src and test gives identical output before and after, and the 13 sites that need braces only because of this false positive lint clean once unbraced.

🤖 Generated with Claude Code

The readability/braces check for unbraced if and else bodies takes the
first semicolon after the condition as the end of the body. When the body
is a for loop with a classic header, or an if with an init-statement, that
semicolon sits inside the nested header. The rest of the header then reads
as a second statement, and the check reports "If/else bodies with multiple
statements require braces" for what is a single statement.

The check now steps over the parenthesized header of a nested if, for,
while or switch before it looks for the semicolon, repeatedly, so a chain
like `if (a) for (...) if (b) x;` works too. Two statements under such a
header are still reported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant