Skip to content

Commit 22fc2cb

Browse files
authored
[clang-format] Fix AllowShortLambdasOnASingleLine interfering with lambda brace wrapping. (#81848)
Fix an issue where the lambda body left brace could sometimes fail to be wrapped when AllowShortLambdasOnASingleLine is enabled. Now, when BraceWrapping.BeforeLambdaBody is enabled, if the brace is not wrapped, we prevent breaks in the lambda body. Resolves #81845
1 parent 44b1767 commit 22fc2cb

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
18031803
}
18041804

18051805
if (Current.MatchingParen && Current.is(BK_Block)) {
1806-
moveStateToNewBlock(State);
1806+
moveStateToNewBlock(State, Newline);
18071807
return;
18081808
}
18091809

@@ -1991,7 +1991,7 @@ void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
19911991
}
19921992
}
19931993

1994-
void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
1994+
void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) {
19951995
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
19961996
State.NextToken->is(TT_LambdaLBrace) &&
19971997
!State.Line->MightBeFunctionDecl) {
@@ -2003,10 +2003,18 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
20032003
NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
20042004
? Style.ObjCBlockIndentWidth
20052005
: Style.IndentWidth);
2006+
2007+
// Even when wrapping before lambda body, the left brace can still be added to
2008+
// the same line. This occurs when checking whether the whole lambda body can
2009+
// go on a single line. In this case we have to make sure there are no line
2010+
// breaks in the body, otherwise we could just end up with a regular lambda
2011+
// body without the brace wrapped.
2012+
bool NoLineBreak = Style.BraceWrapping.BeforeLambdaBody && !NewLine &&
2013+
State.NextToken->is(TT_LambdaLBrace);
2014+
20062015
State.Stack.push_back(ParenState(State.NextToken, NewIndent,
20072016
State.Stack.back().LastSpace,
2008-
/*AvoidBinPacking=*/true,
2009-
/*NoLineBreak=*/false));
2017+
/*AvoidBinPacking=*/true, NoLineBreak));
20102018
State.Stack.back().NestedBlockIndent = NestedBlockIndent;
20112019
State.Stack.back().BreakBeforeParameter = true;
20122020
}

clang/lib/Format/ContinuationIndenter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class ContinuationIndenter {
104104
/// Update 'State' according to the next token being one of ")>}]".
105105
void moveStatePastScopeCloser(LineState &State);
106106
/// Update 'State' with the next token opening a nested block.
107-
void moveStateToNewBlock(LineState &State);
107+
void moveStateToNewBlock(LineState &State, bool NewLine);
108108

109109
/// Reformats a raw string literal.
110110
///

clang/unittests/Format/FormatTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22893,6 +22893,19 @@ TEST_F(FormatTest, FormatsLambdas) {
2289322893
LLVMWithBeforeLambdaBody);
2289422894
verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
2289522895
LLVMWithBeforeLambdaBody);
22896+
verifyFormat(
22897+
"FctWithTwoParams_SLS_All(\n"
22898+
" 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
22899+
LLVMWithBeforeLambdaBody);
22900+
verifyFormat(
22901+
"FctWithTwoParams_SLS_All(\n"
22902+
" 87,\n"
22903+
" []()\n"
22904+
" {\n"
22905+
" return "
22906+
"LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
22907+
" });",
22908+
LLVMWithBeforeLambdaBody);
2289622909
verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
2289722910
LLVMWithBeforeLambdaBody);
2289822911
verifyFormat(

0 commit comments

Comments
 (0)