clang-format BraceWrapping.BeforeLambdaBody: True overrides AllowShortLambdasOnASingleLine: All
Found a partial workaround here: https://stackoverflow.com/questions/67547349/clang-format-lambda-on-single-line-doesnt-work
It should be possible to have multi-line lambdas require Allman style bracing, but have short lambdas truly be a single line.
Suggest switching BeforeLambdaBody from a boolean to a tri-state (Never, OnlyMultiline, Always). With OnlyMultiLine having the new behavior I'm looking for:
auto oneliner = [](auto x){ return 2*x; };
auto multiliner = [](auto x)
{
if (x % 2 == 0)
{
return x*2;
}
return x*3;
};
Works as expected with 20.1.2.
@owenca Why is it expected? It's not possible to have short lambdas and wrapping before the lambda body at the same time.
$ cat a.cc
auto oneliner = [](auto x){ return 2*x; };
auto multiliner = [](auto x)
{
if (x % 2 == 0)
{
return x*2;
}
return x*3;
};
$ clang-format -version
clang-format version 20.1.5
$ clang-format -style='{BreakBeforeBraces: Allman}' a.cc
auto oneliner = [](auto x) { return 2 * x; };
auto multiliner = [](auto x)
{
if (x % 2 == 0)
{
return x * 2;
}
return x * 3;
};
This is what @d3matt was looking for.
Hi @owenca
I think I found the culprit, there seems to be an incompatibility between BraceWrapping/BeforeLambdaBody: true and ColumnLimit: 0. Do you agree that this behaviour is wrong?
Thanks for having a look!
$ cat a.cc
auto oneliner = [](auto x){ return 2*x; };
auto multiliner = [](auto x)
{
if (x % 2 == 0)
{
return x*2;
}
return x*3;
};
$ clang-format --version
Ubuntu clang-format version 21.1.8 (6ubuntu1)
$ clang-format -style='{BreakBeforeBraces: Custom, BraceWrapping: {BeforeLambdaBody: true}, AllowShortLambdasOnASingleLine: true }' a.cc
auto oneliner = [](auto x) { return 2 * x; };
auto multiliner = [](auto x)
{
if (x % 2 == 0) {
return x * 2;
}
return x * 3;
};
$ clang-format -style='{BreakBeforeBraces: Custom, BraceWrapping: {BeforeLambdaBody: true}, AllowShortLambdasOnASingleLine: true, ColumnLimit: 0 }' a.cc
auto oneliner = [](auto x)
{ return 2 * x; };
auto multiliner = [](auto x)
{
if (x % 2 == 0) {
return x * 2;
}
return x * 3;
};
I would like to take this on. Having recently worked on related clang-format pr.