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 @@ -330,7 +330,7 @@ const FormatToken &Current = *State.NextToken; const FormatToken &Previous = *Current.Previous; if (Style.BraceWrapping.BeforeLambdaBody && Current.CanBreakBefore && - Current.is(TT_LambdaLBrace)) { + Current.is(TT_LambdaLBrace) && Previous.isNot(TT_LineComment)) { auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack); return (LambdaBodyLength > getColumnLimit(State)); } 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 @@ -14587,6 +14587,30 @@ LLVMWithBeforeLambdaBody); } +TEST_F(FormatTest, LambdaWithLineComments) { + FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); + LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; + LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; + LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = + FormatStyle::ShortLambdaStyle::SLS_All; + + verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); + verifyFormat("auto k = []() // comment\n" + "{ return; }", + LLVMWithBeforeLambdaBody); + verifyFormat("auto k = []() /* comment */ { return; }", + LLVMWithBeforeLambdaBody); + verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", + LLVMWithBeforeLambdaBody); + verifyFormat("auto k = []() // X\n" + "{ return; }", + LLVMWithBeforeLambdaBody); + verifyFormat( + "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" + "{ return; }", + LLVMWithBeforeLambdaBody); +} + TEST_F(FormatTest, EmptyLinesInLambdas) { verifyFormat("auto lambda = []() {\n" " x(); //\n"