Index: clang/lib/Format/ContinuationIndenter.cpp =================================================================== --- clang/lib/Format/ContinuationIndenter.cpp +++ clang/lib/Format/ContinuationIndenter.cpp @@ -331,6 +331,15 @@ if (Previous.is(tok::l_square) && Previous.is(TT_ObjCMethodExpr)) return false; + if (Current.is(TT_ConditionalExpr) && Previous.is(tok::r_paren) && + Previous.MatchingParen && Previous.MatchingParen->Previous && + Previous.MatchingParen->Previous->is(tok::r_brace) && + Previous.MatchingParen->Previous->MatchingParen && + Previous.MatchingParen->Previous->MatchingParen->is(TT_LambdaLBrace)) { + // We have a lambda within a conditional expression, allow breaking here. + return true; + } + return !CurrentState.NoLineBreak; } Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -26549,6 +26549,54 @@ Style); } +TEST_F(FormatTest, MultilineLambdaInConditional) { + auto Style = getLLVMStyleWithColumns(70); + verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n" + " ;\n" + " return 5;\n" + "}()\n" + " : 2;", + Style); + verifyFormat( + "auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? 2 : []() {\n" + " ;\n" + " return 5;\n" + "}();", + Style); + + Style = getLLVMStyleWithColumns(60); + verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n" + " ? []() {\n" + " ;\n" + " return 5;\n" + " }()\n" + " : 2;", + Style); + verifyFormat("auto aLengthyIdentifier =\n" + " oneExpressionSoThatWeBreak ? 2 : []() {\n" + " ;\n" + " return 5;\n" + " }();", + Style); + + Style = getLLVMStyleWithColumns(40); + verifyFormat("auto aLengthyIdentifier =\n" + " oneExpressionSoThatWeBreak ? []() {\n" + " ;\n" + " return 5;\n" + " }()\n" + " : 2;", + Style); + verifyFormat("auto aLengthyIdentifier =\n" + " oneExpressionSoThatWeBreak\n" + " ? 2\n" + " : []() {\n" + " ;\n" + " return 5;\n" + " };", + Style); +} + TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { auto Style = getLLVMStyle();