Index: clang/lib/Format/ContinuationIndenter.cpp =================================================================== --- clang/lib/Format/ContinuationIndenter.cpp +++ clang/lib/Format/ContinuationIndenter.cpp @@ -1419,9 +1419,14 @@ Previous && Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) && !Previous->is(TT_DictLiteral) && State.Stack.size() > 1 && !CurrentState.HasMultipleNestedBlocks) { - if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline) + if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline && + // Do not forbid line breaks for directly invoced lambdas. + (!Current.MatchingParen || + Current.MatchingParen->isNot(TT_LambdaLBrace) || !Current.Next || + Current.Next->isNot(tok::l_paren))) { for (ParenState &PState : llvm::drop_end(State.Stack)) PState.NoLineBreak = true; + } State.Stack[State.Stack.size() - 2].NestedBlockInlined = false; } if (Previous && (Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) || Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -26379,6 +26379,33 @@ Style); } +TEST_F(FormatTest, MultilineLambdaInConditional) { + auto Style = getLLVMStyleWithColumns(70); + verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n" + " ;\n" + " return 5;\n" + "}()\n" + " : 2;", + Style); + + Style = getLLVMStyleWithColumns(60); + verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n" + " ? []() {\n" + " ;\n" + " return 5;\n" + " }()\n" + " : 2;", + Style); + + Style = getLLVMStyleWithColumns(40); + verifyFormat("auto aLengthyIdentifier =\n" + " oneExpressionSoThatWeBreak ? []() {\n" + " ;\n" + " return 5;\n" + " }()\n" + " : 2;", Style); +} + TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { auto Style = getLLVMStyle();