InaccurateEraseCheck.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //===--- InaccurateEraseCheck.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_BUGPRONE_INACCURATEERASECHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INACCURATEERASECHECK_H
  10. #include "../ClangTidyCheck.h"
  11. namespace clang::tidy::bugprone {
  12. /// Checks for inaccurate use of the `erase()` method.
  13. ///
  14. /// Algorithms like `remove()` do not actually remove any element from the
  15. /// container but return an iterator to the first redundant element at the end
  16. /// of the container. These redundant elements must be removed using the
  17. /// `erase()` method. This check warns when not all of the elements will be
  18. /// removed due to using an inappropriate overload.
  19. class InaccurateEraseCheck : public ClangTidyCheck {
  20. public:
  21. InaccurateEraseCheck(StringRef Name, ClangTidyContext *Context)
  22. : ClangTidyCheck(Name, Context) {}
  23. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
  24. return LangOpts.CPlusPlus;
  25. }
  26. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  27. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  28. std::optional<TraversalKind> getCheckTraversalKind() const override {
  29. return TK_IgnoreUnlessSpelledInSource;
  30. }
  31. };
  32. } // namespace clang::tidy::bugprone
  33. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_INACCURATEERASECHECK_H