diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5499,7 +5499,7 @@ if (Next && Next->is(tok::l_paren)) return false; const FormatToken *Previous = Right.MatchingParen->Previous; - return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf())); + return !(Previous && Previous->is(tok::kw_for)); } // Allow breaking after a trailing annotation, e.g. after a method 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 @@ -25494,12 +25494,56 @@ "}", Style); + // Treating if clauses as block indents causes a known bug (#54808, #63383) + // breaking the following test. It gets formatted instead as: + // "if (quitelongarg != (alsolongarg - 1)\n" + // ") { // ABC is a very longgggggggggggg comment" + // "return;" + // "}" + +#if 0 verifyFormat("if (quitelongarg !=\n" " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " "comment\n" " return;\n" "}", Style); +#endif + + verifyFormat("void foo() {\n" + " if (quitelongname < alsolongname ||\n" + " anotherevenlongername <=\n" + " thisreallyreallyreallyreallyreallyreallylongername ||" + "\n" + " othername < thislastname) {\n" + " return;\n" + " } else if (\n" + " quitelongname < alsolongname ||\n" + " anotherevenlongername <=\n" + " thisreallyreallyreallyreallyreallyreallylongername ||" + "\n" + " othername < thislastname\n" + " ) {\n" + " return;\n" + " }\n" + "}", + Style); + + Style.ContinuationIndentWidth = 2; + verifyFormat("void foo() {\n" + " if (\n" + " ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n" + " ontoMultipleLines && whenFormattedCorrectly\n" + " ) {\n" + " if (false) {\n" + " } else if (\n" + " thisIsRatherALongIfClause && thatIExpectToBeBroken ||\n" + " ontoMultipleLines && whenFormattedCorrectly\n" + " ) {\n" + " }\n" + " }\n" + "}", + Style); } TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {