diff --git a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h --- a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h +++ b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNIQUEPTRRESETRELEASECHECK_H #include "../ClangTidyCheck.h" +#include "../utils/IncludeInserter.h" namespace clang { namespace tidy { @@ -28,8 +29,7 @@ /// be `std::unique_ptr*`. class UniqueptrResetReleaseCheck : public ClangTidyCheck { public: - UniqueptrResetReleaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + UniqueptrResetReleaseCheck(StringRef Name, ClangTidyContext *Context); bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { // Only register the matchers for C++11; the functionality currently does @@ -37,8 +37,13 @@ // provide any benefit to other languages, despite being benign. return LangOpts.CPlusPlus11; } + void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, + Preprocessor *ModuleExpanderPP) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + utils::IncludeInserter Inserter; }; } // namespace misc diff --git a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp --- a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp @@ -16,6 +16,17 @@ namespace tidy { namespace misc { +UniqueptrResetReleaseCheck::UniqueptrResetReleaseCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + Inserter(Options.getLocalOrGlobal("IncludeStyle", + utils::IncludeSorter::IS_LLVM)) {} + +void UniqueptrResetReleaseCheck::registerPPCallbacks( + const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { + Inserter.registerPreprocessor(PP); +} + void UniqueptrResetReleaseCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxMemberCallExpr( @@ -119,15 +130,18 @@ std::string NewText = LeftText + " = " + RightText; - diag(ResetMember->getExprLoc(), - "prefer ptr = %select{std::move(ptr2)|ReturnUnique()}0 over " - "ptr.reset(%select{ptr2|ReturnUnique()}0.release())") - << !IsMove - << FixItHint::CreateReplacement( - CharSourceRange::getTokenRange(ResetCall->getSourceRange()), - NewText); + auto D = diag(ResetMember->getExprLoc(), + "prefer ptr = %select{std::move(ptr2)|ReturnUnique()}0 over " + "ptr.reset(%select{ptr2|ReturnUnique()}0.release())") + << !IsMove + << FixItHint::CreateReplacement( + CharSourceRange::getTokenRange(ResetCall->getSourceRange()), + NewText); + if (IsMove) + D << Inserter.createIncludeInsertion( + Result.SourceManager->getFileID(ResetMember->getBeginLoc()), + ""); } - } // namespace misc } // namespace tidy } // namespace clang diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst b/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst --- a/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst @@ -14,3 +14,11 @@ If ``y`` is already rvalue, ``std::move()`` is not added. ``x`` and ``y`` can also be ``std::unique_ptr*``. + +Options +------- + +.. option:: IncludeStyle + + A string specifying which include-style is used, `llvm` or `google`. Default + is `llvm`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp @@ -1,5 +1,7 @@ // RUN: %check_clang_tidy %s misc-uniqueptr-reset-release %t +// CHECK-FIXES: #include + namespace std { template