njames93 created this revision. njames93 added reviewers: alexfh, aaron.ballman. Herald added a subscriber: xazax.hun. njames93 requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
This is the only remaining check that creates `std::move` includes but doesn't add a `<utility>` include. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D97683 Files: clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp +++ 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 <utility> + namespace std { template <typename T> Index: clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst =================================================================== --- clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst +++ 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<Foo>*``. + +Options +------- + +.. option:: IncludeStyle + + A string specifying which include-style is used, `llvm` or `google`. Default + is `llvm`. Index: clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h =================================================================== --- clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h +++ 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<Foo>*`. 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 Index: clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp +++ 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()), + "<utility>"); } - } // namespace misc } // namespace tidy } // namespace clang
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits