Skip to content

[clang-format] Fix AllowShortLambdasOnASingleLine interfering with lambda brace wrapping. #81848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
}

if (Current.MatchingParen && Current.is(BK_Block)) {
moveStateToNewBlock(State);
moveStateToNewBlock(State, Newline);
return;
}

Expand Down Expand Up @@ -1981,7 +1981,7 @@ void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
}
}

void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) {
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
State.NextToken->is(TT_LambdaLBrace) &&
!State.Line->MightBeFunctionDecl) {
Expand All @@ -1993,10 +1993,18 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
? Style.ObjCBlockIndentWidth
: Style.IndentWidth);

// Even when wrapping before lambda body, the left brace can still be added to
// the same line. This occurs when checking whether the whole lambda body can
// go on a single line. In this case we have to make sure there are no line
// breaks in the body, otherwise we could just end up with a regular lambda
// body without the brace wrapped.
bool NoLineBreak = Style.BraceWrapping.BeforeLambdaBody && !NewLine &&
State.NextToken->is(TT_LambdaLBrace);

State.Stack.push_back(ParenState(State.NextToken, NewIndent,
State.Stack.back().LastSpace,
/*AvoidBinPacking=*/true,
/*NoLineBreak=*/false));
/*AvoidBinPacking=*/true, NoLineBreak));
State.Stack.back().NestedBlockIndent = NestedBlockIndent;
State.Stack.back().BreakBeforeParameter = true;
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/ContinuationIndenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ContinuationIndenter {
/// Update 'State' according to the next token being one of ")>}]".
void moveStatePastScopeCloser(LineState &State);
/// Update 'State' with the next token opening a nested block.
void moveStateToNewBlock(LineState &State);
void moveStateToNewBlock(LineState &State, bool NewLine);

/// Reformats a raw string literal.
///
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22893,6 +22893,19 @@ TEST_F(FormatTest, FormatsLambdas) {
LLVMWithBeforeLambdaBody);
verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);",
LLVMWithBeforeLambdaBody);
verifyFormat(
"FctWithTwoParams_SLS_All(\n"
" 87, []() { return LongLineThatWillForceBothParamsToNewLine(); });",
LLVMWithBeforeLambdaBody);
verifyFormat(
"FctWithTwoParams_SLS_All(\n"
" 87,\n"
" []()\n"
" {\n"
" return "
"LongLineThatWillForceTheLambdaBodyToBeBrokenIntoMultipleLines();\n"
" });",
LLVMWithBeforeLambdaBody);
verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });",
LLVMWithBeforeLambdaBody);
verifyFormat(
Expand Down