UniqueptrResetReleaseCheck.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //===--- UniqueptrResetReleaseCheck.h - clang-tidy --------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNIQUEPTRRESETRELEASECHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNIQUEPTRRESETRELEASECHECK_H
  10. #include "../ClangTidyCheck.h"
  11. #include "../utils/IncludeInserter.h"
  12. namespace clang::tidy::misc {
  13. /// Find and replace `unique_ptr::reset(release())` with `std::move()`.
  14. ///
  15. /// Example:
  16. ///
  17. /// \code
  18. /// std::unique_ptr<Foo> x, y;
  19. /// x.reset(y.release()); -> x = std::move(y);
  20. /// \endcode
  21. ///
  22. /// If `y` is already rvalue, `std::move()` is not added. `x` and `y` can also
  23. /// be `std::unique_ptr<Foo>*`.
  24. class UniqueptrResetReleaseCheck : public ClangTidyCheck {
  25. public:
  26. UniqueptrResetReleaseCheck(StringRef Name, ClangTidyContext *Context);
  27. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
  28. // Only register the matchers for C++11; the functionality currently does
  29. // not
  30. // provide any benefit to other languages, despite being benign.
  31. return LangOpts.CPlusPlus11;
  32. }
  33. void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  34. Preprocessor *ModuleExpanderPP) override;
  35. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  36. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  37. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  38. private:
  39. utils::IncludeInserter Inserter;
  40. };
  41. } // namespace clang::tidy::misc
  42. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNIQUEPTRRESETRELEASECHECK_H