From: Daniel Jasper Date: Wed, 9 Apr 2014 12:21:48 +0000 (+0000) Subject: clang-format: Fix bug where clang-format would break the code. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8d475618089e8bb3d0e47605b236b445eaefab2d;p=clang clang-format: Fix bug where clang-format would break the code. Before, it would turn: SomeFunction([]() { // Cool function.. return 43; }); Into this: SomeFunction([]() { // Cool function.. return 43; }); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205849 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 5367106535..4075ccdc8c 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1121,6 +1121,10 @@ private: if (Previous.Children.size() > 1) return false; + // Cannot merge into one line if this line ends on a comment. + if (Previous.is(tok::comment)) + return false; + // We can't put the closing "}" on a line with a trailing comment. if (Previous.Children[0]->Last->isTrailingComment()) return false; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index b932b274d7..5f44c5bb2b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -8161,6 +8161,9 @@ TEST_F(FormatTest, FormatsLambdas) { " x.end(), //\n" " [&](int, int) { return 1; });\n" "}\n"); + verifyFormat("SomeFunction([]() { // A cool function...\n" + " return 43;\n" + "});"); // Lambdas with return types. verifyFormat("int c = []() -> int { return 2; }();\n");