diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 758dc5860888ec..55e0b7f8e8d9ea 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -406,7 +406,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End, Changes[i].Spaces += Shift; // We should not remove required spaces unless we break the line before. - assert(Changes[i].NewlinesBefore > 0 || + assert(Shift >= 0 || Changes[i].NewlinesBefore > 0 || Changes[i].Spaces >= static_cast(Changes[i].Tok->SpacesRequiredBefore) || Changes[i].Tok->is(tok::eof)); diff --git a/clang/unittests/Format/FormatTestSelective.cpp b/clang/unittests/Format/FormatTestSelective.cpp index c88d1b8bd8ba2a..2725e4cf776f62 100644 --- a/clang/unittests/Format/FormatTestSelective.cpp +++ b/clang/unittests/Format/FormatTestSelective.cpp @@ -603,6 +603,14 @@ TEST_F(FormatTestSelective, KeepsIndentAfterCommentSectionImport) { EXPECT_EQ(Code, format(Code, 47, 1)); } +TEST_F(FormatTestSelective, DontAssert) { + // https://llvm.org/PR53880 + std::string Code = "void f() {\n" + " return a == 8 ? 32 : 16;\n" + "}\n"; + EXPECT_EQ(Code, format(Code, 40, 0)); +} + } // end namespace } // end namespace format } // end namespace clang