Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -2021,7 +2021,15 @@ parseBlock(/*MustBeDeclaration=*/false); addUnwrappedLine(); } else if (FormatTok->Tok.is(tok::kw_if)) { + FormatToken *Previous = AllTokens[Tokens->getPosition() - 1]; + bool PrecededByComment = Previous->is(tok::comment); + if (PrecededByComment) { + addUnwrappedLine(); + ++Line->Level; + } parseIfThenElse(); + if (PrecededByComment) + --Line->Level; } else { addUnwrappedLine(); ++Line->Level; Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -1181,6 +1181,13 @@ " g();\n" "else\n" " h();"); + verifyFormat("if (a)\n" + " f();\n" + "else // comment\n" + " if (b) {\n" + " g();\n" + " h()\n" + " }"); verifyFormat("if constexpr (a)\n" " f();\n" "else if constexpr (b)\n"