diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1898,8 +1898,9 @@ assert(Token->isOneOf(tok::l_brace, tok::r_brace)); assert(Token->Next || Token == Line->Last); const auto Start = - Token == Line->Last || (Token->Next->is(tok::kw_else) && - Token->Next->NewlinesBefore > 0) + Token == Line->Last || + (Token->Next->isOneOf(tok::kw_else, tok::comment) && + Token->Next->NewlinesBefore > 0) ? Token->WhitespaceRange.getBegin() : Token->Tok.getLocation(); const auto Range = 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 @@ -25348,6 +25348,20 @@ "}", Style); + verifyFormat("if (a)\n" + " foo();\n" + "// comment\n" + "else\n" + " bar();", + "if (a) {\n" + " foo();\n" + "}\n" + "// comment\n" + "else {\n" + " bar();\n" + "}", + Style); + verifyFormat("if (a) {\n" "Label:\n" "}",