According to clang-format documentation, the value WithoutElse of the option AllowShortIfStatementsOnASingleLine is expected to "put short ifs on the same line only if the else is not a compound statement". However, with the current code, and the option AllowShortBlocksOnASingleLine: Always, a short if block is always put on a single line whatever the following else statement is compound or not, and a short if statement is never put on a single line whatever the following else statement is compound or not. With this patch, the formatting is as documented.
Fixes #45338
If this patch fixes the simple example given on the documentation, the formatting or more complex code with else if and nested if (or loops) is not what I would expect from this option. Here are some formatted examples (with the options {BasedOnStyle: google, BreakBeforeBraces: Allman, AllowShortBlocksOnASingleLine: Always, AllowShortIfStatementsOnASingleLine: WithoutElse} and proposed patch):
int main() { if (true) { function(); } if (true) function(); if (true) { function(); } else function(); if (true) { function(); } else { function(); } if (true) function(); else function(); if (true) function(); else { function(); } if (true) function(); else if (false) function(); else { function(); } if (true) { function(); } else if (false) function(); else function(); if (true) function(); else if (false) { function(); } else function(); if (true) function(); else if (true) if (false) function(); if (true) function(); else if (true) if (false) { function(); } if (true) { function(); } else if (true) if (false) function(); if (true) { function(); } else if (true) if (false) { function(); } }
The first behaviour I would expect is that a short if statement is either never put in a short line if there is more than one else or only put on a single line when none of the else statement are compound. The second behaviour I would expect is that a nested if (or loop) is considered as a compound statement and prevents the is statement to be put in a single line.
So I'd only expect this to get merged if it didn't have an else