Index: clang/lib/Format/QualifierAlignmentFixer.cpp =================================================================== --- clang/lib/Format/QualifierAlignmentFixer.cpp +++ clang/lib/Format/QualifierAlignmentFixer.cpp @@ -67,14 +67,12 @@ NextStartColumn, LastStartColumn); llvm::Optional CurrentCode = None; tooling::Replacements Fixes; - unsigned Penalty = 0; for (size_t I = 0, E = Passes.size(); I < E; ++I) { std::pair PassFixes = Passes[I](*Env); auto NewCode = applyAllReplacements( CurrentCode ? StringRef(*CurrentCode) : Code, PassFixes.first); if (NewCode) { Fixes = Fixes.merge(PassFixes.first); - Penalty += PassFixes.second; if (I + 1 < E) { CurrentCode = std::move(*NewCode); Env = std::make_unique( @@ -84,7 +82,21 @@ } } } - return {Fixes, Penalty}; + + // Don't make replacements that replace nothing. + tooling::Replacements NonNoOpFixes; + + for (auto I = Fixes.begin(), E = Fixes.end(); I != E; ++I) { + StringRef OriginalCode = Code.substr(I->getOffset(), I->getLength()); + + if (!OriginalCode.equals(I->getReplacementText())) { + auto Err = NonNoOpFixes.add(*I); + if (Err) + llvm::errs() << "Error adding replacements : " + << llvm::toString(std::move(Err)) << "\n"; + } + } + return {NonNoOpFixes, 0}; } static void replaceToken(const SourceManager &SourceMgr, Index: clang/unittests/Format/QualifierFixerTest.cpp =================================================================== --- clang/unittests/Format/QualifierFixerTest.cpp +++ clang/unittests/Format/QualifierFixerTest.cpp @@ -806,5 +806,17 @@ "Foo(unsigned const char *bytes)", Style); } +TEST_F(QualifierFixerTest, NoOpQualifierReplacements) { + + FormatStyle Style = getLLVMStyle(); + Style.QualifierAlignment = FormatStyle::QAS_Custom; + Style.QualifierOrder = {"static", "const", "type"}; + + ReplacementCount = 0; + EXPECT_EQ(ReplacementCount, 0); + verifyFormat("static const uint32 foo[] = {0, 31};", Style); + EXPECT_EQ(ReplacementCount, 0); +} + } // namespace format } // namespace clang