Skip to content

Commit a727440

Browse files
committed
[clang-format] Stop moving lambda to new line only to indent it more.
Hanging indents are minimised for lambdas by pushing them onto a new line. However, it could still do this even if it would cause even more hanging indents than it otherwise would. The handling has been expanded to check for this case and avoid moving the lambda to a new line.
1 parent f8d6316 commit a727440

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,30 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
325325
if (Current.isMemberAccess() && CurrentState.ContainsUnwrappedBuilder)
326326
return false;
327327

328-
// Don't create a 'hanging' indent if there are multiple blocks in a single
329-
// statement and we are aligning lambda blocks to their signatures.
330-
if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
328+
// Force a lambda onto a new line so that we don't create a 'hanging' indent
329+
// if there are multiple blocks in a single statement and we are aligning
330+
// lambda blocks to their signatures.
331+
if (Previous.is(tok::l_brace) && State.Stack.size() > 2 &&
331332
State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
332333
State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks &&
333334
Style.LambdaBodyIndentation == FormatStyle::LBI_Signature) {
334-
return false;
335+
if (!Style.isCpp())
336+
return false;
337+
338+
// Make sure to push lambdas to a new line when they are an argument with
339+
// other arguments preceding them.
340+
if (State.Stack[State.Stack.size() - 2].StartOfFunctionCall > 0)
341+
return false;
342+
343+
// Only force a new line if it is not just going to create a worse hanging
344+
// indent. Otherwise, based on the ContinuationIndentWidth, we could end up
345+
// more indented than we would've been. To avoid odd looking breaks, make
346+
// sure we save at least IndentWidth.
347+
if (State.Stack[State.Stack.size() - 3].Indent +
348+
Style.ContinuationIndentWidth + Style.IndentWidth <
349+
State.Stack[State.Stack.size() - 2].Indent) {
350+
return false;
351+
}
335352
}
336353

337354
// Don't break after very short return types (e.g. "void") as that is often

clang/unittests/Format/FormatTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23814,6 +23814,20 @@ TEST_F(FormatTest, FormatsLambdas) {
2381423814
"}",
2381523815
LLVMWithBeforeLambdaBody);
2381623816

23817+
// Make sure we don't put the lambda on a new line when it would be indented
23818+
// more than where it would be otherwise.
23819+
verifyFormat("if ([]()\n"
23820+
" {\n"
23821+
" return true;\n"
23822+
" }()) {\n"
23823+
"}",
23824+
LLVMWithBeforeLambdaBody);
23825+
verifyFormat("fun([]()\n"
23826+
" {\n"
23827+
" return 17;\n"
23828+
" });",
23829+
LLVMWithBeforeLambdaBody);
23830+
2381723831
LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
2381823832
FormatStyle::ShortLambdaStyle::SLS_Empty;
2381923833
verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n"

0 commit comments

Comments
 (0)