diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/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->MatchingParen && + Previous.MatchingParen->Previous->MatchingParen->is(TT_LambdaLBrace)) { + // We have a lambda within a conditional expression, allow breaking here. + assert(Previous.MatchingParen->Previous->is(tok::r_brace)); + return true; + } + return !CurrentState.NoLineBreak; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -25549,6 +25549,54 @@ verifyFormat("template struct Foo {};", 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();