| Issue |
237211
|
| Summary |
[clang-format] AllowShortFunctionsOnASingleLine doesn't merge one-statement bodies containing braces
|
| Labels |
clang-format
|
| Assignees |
|
| Reporter |
jmr
|
With `AllowShortFunctionsOnASingleLine: All` (the LLVM default), a function with a one-statement body that fits in the column limit still gets split onto three lines if the statement has a lambda or a braced block in it. That's true even when the lambda or block on its own is allowed on one line by `AllowShortLambdasOnASingleLine` (`All` by default in LLVM style) or `AllowShortBlocksOnASingleLine`.
Reproduced on current main (54fe44c9d1a4).
### Short lambda in a short function
```console
$ clang-format --style=LLVM <<_
void f() { [] { return y; }(); }
_
void f() {
[] { return y; }();
}
```
Expected: `void f() { [] { return y; }(); }`
`--style=Google` gives the same result.
### Short braced `if` in a short function
```console
$ clang-format --style='{
BasedOnStyle: LLVM,
AllowShortIfStatementsOnASingleLine: WithoutElse,
AllowShortBlocksOnASingleLine: Always
}' <<_
void f() { if (x) { return y; } }
_
void f() {
if (x) { return y; }
}
```
Expected: `void f() { if (x) { return y; } }`
### For comparison
Braced init lists don't block merging:
```console
$ clang-format --style=LLVM <<_
void f() { S s = {1}; }
_
void f() { S s = {1}; }
```
### Why this looks like a bug
The [documentation](https://clang.llvm.org/docs/ClangFormatStyleOptions.html#allowshortfunctionsonasingleline) describes the option as:
> Different styles for merging short functions containing at most one statement.
and `All` as:
> Merge all functions fitting on a single line.
Both examples have one statement and fit on one line, and the statement already gets put on one line by itself. The docs say nothing about the statement also having to be "simple" or brace-free, so I'd expect these to be merged.
### Cause
`LineJoiner::tryMergeSimpleBlock` rejects any body line that contains a `{` or `}` that isn't part of a braced init list ([UnwrappedLineFormatter.cpp#L1046-L1060](https://github.com/llvm/llvm-project/blob/54fe44c9d1a4b8cb1a5df66b2fc3be6214d2bc30/clang/lib/Format/UnwrappedLineFormatter.cpp#L1046-L1060)):
```cpp
// Second, check that the next line does not contain non-braced-init
// braces - if it does, readability declines when putting it into a
// single line.
```
So the brace check is deliberate, and @r4nt said in https://github.com/llvm/llvm-project/pull/234728#issuecomment-6364534294 that only simple bodies were ever meant to be merged. If that's the intent, the docs should say so. Nothing in "short functions containing at most one statement" or "Merge all functions fitting on a single line" tells you these cases are excluded. If it's not the intent, the rule could be: merge when the body formats as one line and the whole function fits in the column limit.
See also #237208, which has the same symptom for an unbraced `if` but a different cause.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs