Skip to content

Commit 14c8c07

Browse files
jp4a50legrosbuffle
authored andcommitted
[clang-format] Disable OuterScope lambda indentation behaviour for constructor initializers (llvm#66755)
By default, OuterScope aligns lambdas to the beginning of the current line. This makes sense for most types of statements within code blocks but leads to unappealing and misleading indentation for lambdas within constructor initializers.
1 parent e1f6f8f commit 14c8c07

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

clang/docs/ClangFormatStyleOptions.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -3788,8 +3788,8 @@ the configuration (without a prefix: ``Auto``).
37883788
});
37893789

37903790
* ``LBI_OuterScope`` (in configuration: ``OuterScope``)
3791-
Align lambda body relative to the indentation level of the outer scope
3792-
the lambda signature resides in.
3791+
For statements within block scope, align lambda body relative to the
3792+
indentation level of the outer scope the lambda signature resides in.
37933793

37943794
.. code-block:: c++
37953795

clang/include/clang/Format/Format.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -2879,8 +2879,8 @@ struct FormatStyle {
28792879
/// });
28802880
/// \endcode
28812881
LBI_Signature,
2882-
/// Align lambda body relative to the indentation level of the outer scope
2883-
/// the lambda signature resides in.
2882+
/// For statements within block scope, align lambda body relative to the
2883+
/// indentation level of the outer scope the lambda signature resides in.
28842884
/// \code
28852885
/// someMethod(
28862886
/// [](SomeReallyLongLambdaSignatureArgument foo) {

clang/lib/Format/ContinuationIndenter.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1955,7 +1955,8 @@ void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
19551955

19561956
void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
19571957
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
1958-
State.NextToken->is(TT_LambdaLBrace)) {
1958+
State.NextToken->is(TT_LambdaLBrace) &&
1959+
!State.Line->MightBeFunctionDecl) {
19591960
State.Stack.back().NestedBlockIndent = State.FirstIndent;
19601961
}
19611962
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;

clang/unittests/Format/FormatTest.cpp

+46-7
Original file line numberDiff line numberDiff line change
@@ -22537,10 +22537,12 @@ TEST_F(FormatTest, FormatsLambdas) {
2253722537
" }\n"
2253822538
"}",
2253922539
Style);
22540-
verifyFormat("std::sort(v.begin(), v.end(),\n"
22541-
" [](const auto &foo, const auto &bar) {\n"
22542-
" return foo.baz < bar.baz;\n"
22543-
"});",
22540+
verifyFormat("void test() {\n"
22541+
" std::sort(v.begin(), v.end(),\n"
22542+
" [](const auto &foo, const auto &bar) {\n"
22543+
" return foo.baz < bar.baz;\n"
22544+
" });\n"
22545+
"};",
2254422546
Style);
2254522547
verifyFormat("void test() {\n"
2254622548
" (\n"
@@ -22566,6 +22568,12 @@ TEST_F(FormatTest, FormatsLambdas) {
2256622568
" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2256722569
" }",
2256822570
Style);
22571+
verifyFormat("#define SORT(v) \\\n"
22572+
" std::sort(v.begin(), v.end(), \\\n"
22573+
" [](const auto &foo, const auto &bar) { \\\n"
22574+
" return foo.baz < bar.baz; \\\n"
22575+
" });",
22576+
Style);
2256922577
verifyFormat("void foo() {\n"
2257022578
" aFunction(1, b(c(foo, bar, baz, [](d) {\n"
2257122579
" auto f = e(d);\n"
@@ -22589,9 +22597,40 @@ TEST_F(FormatTest, FormatsLambdas) {
2258922597
verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
2259022598
" AnotherLongClassName baz)\n"
2259122599
" : baz{baz}, func{[&] {\n"
22592-
" auto qux = bar;\n"
22593-
" return aFunkyFunctionCall(qux);\n"
22594-
"}} {}",
22600+
" auto qux = bar;\n"
22601+
" return aFunkyFunctionCall(qux);\n"
22602+
" }} {}",
22603+
Style);
22604+
verifyFormat("void foo() {\n"
22605+
" class Foo {\n"
22606+
" public:\n"
22607+
" Foo()\n"
22608+
" : qux{[](int quux) {\n"
22609+
" auto tmp = quux;\n"
22610+
" return tmp;\n"
22611+
" }} {}\n"
22612+
"\n"
22613+
" private:\n"
22614+
" std::function<void(int quux)> qux;\n"
22615+
" };\n"
22616+
"}",
22617+
Style);
22618+
Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
22619+
verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
22620+
" AnotherLongClassName baz) :\n"
22621+
" baz{baz}, func{[&] {\n"
22622+
" auto qux = bar;\n"
22623+
" return aFunkyFunctionCall(qux);\n"
22624+
" }} {}",
22625+
Style);
22626+
Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
22627+
verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
22628+
" AnotherLongClassName baz) :\n"
22629+
" baz{baz},\n"
22630+
" func{[&] {\n"
22631+
" auto qux = bar;\n"
22632+
" return aFunkyFunctionCall(qux);\n"
22633+
" }} {}",
2259522634
Style);
2259622635
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
2259722636
// FIXME: The following test should pass, but fails at the time of writing.

0 commit comments

Comments
 (0)