diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index bb2dc47f6630..e3e959ef3352 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -888,6 +888,10 @@ private: if (Newlines == 0 && !RootToken.IsFirst) Newlines = 1; + // Remove empty lines after "{". + if (PreviousLine && PreviousLine->Last->is(tok::l_brace)) + Newlines = 1; + // Insert extra new line before access specifiers. if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index e1b72d9a99f7..42b9e856e977 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -174,6 +174,22 @@ TEST_F(FormatTest, RemovesEmptyLines) { "\n" "};")); + // Remove empty lines at the beginning and end of blocks. + EXPECT_EQ("void f() {\n" + " if (a) {\n" + " f();\n" + " }\n" + "}", + format("void f() {\n" + "\n" + " if (a) {\n" + "\n" + " f();\n" + "\n" + " }\n" + "\n" + "}")); + // Don't remove empty lines in more complex control statements. EXPECT_EQ("void f() {\n" " if (a) {\n"