The latest version of clang-format
(that is 10) has configuration options specific to formatting lambdas. Specifically what you are asking for can be achieved by assigning to the configuration directive AllowShortLambdasOnASingleLine
the value SLS_Inline
.
Following are the other options available from the offical documentation:
AllowShortLambdasOnASingleLine (ShortLambdaStyle)
Dependent on the value, auto lambda { return 0; } can be put on a single line.
Possible values:
SLS_None
(in configuration: None) Never merge lambdas into a single line.
SLS_Empty
(in configuration: Empty) Only merge empty lambdas.
auto lambda = [](int a) {}
auto lambda2 = [](int a) {
return a;
};
SLS_Inline
(in configuration: Inline) Merge lambda into a single line if argument of a function.
auto lambda = [](int a) {
return a;
};
sort(a.begin(), a.end(), ()[] { return x < y; })
SLS_All
(in configuration: All) Merge all lambdas fitting on a single line.
auto lambda = [](int a) {}
auto lambda2 = [](int a) { return a; };
I didn't find a way to do the same on the clang-format
shipped with LLVM 8
. The closest configuration I found was the following
clang-format -style={BasedOnStyle: WebKit, AllowShortFunctionsOnASingleLine: true, AllowShortBlocksOnASingleLine: true}
Anyway it does not achieve your full request, this is how it looks on my Windows laptop:
void f() {
func([this] { return false; },
this); // ok
func(this, [this] { return false; }); // ok
func([this] { return false; }); // ok
}