[clang-tools-extra] f2f920b - [clang-tidy] Simplify inaccurate erase check

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 13 05:52:29 PST 2021


Author: Stephen Kelly
Date: 2021-02-13T13:51:27Z
New Revision: f2f920b987f3810863b9d0160cbb52df3fa2ab6f

URL: https://github.com/llvm/llvm-project/commit/f2f920b987f3810863b9d0160cbb52df3fa2ab6f
DIFF: https://github.com/llvm/llvm-project/commit/f2f920b987f3810863b9d0160cbb52df3fa2ab6f.diff

LOG: [clang-tidy] Simplify inaccurate erase check

The normalization of matchers means that this now works in all language
modes.

Differential Revision: https://reviews.llvm.org/D96139

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.cpp
    clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.h
    clang-tools-extra/test/clang-tidy/checkers/bugprone-inaccurate-erase.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.cpp
index 8e5a53280183..96fa7ef48348 100644
--- a/clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.cpp
@@ -22,25 +22,18 @@ void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) {
       callExpr(
           callee(functionDecl(hasAnyName("remove", "remove_if", "unique"))),
           hasArgument(
-              1,
-              anyOf(cxxConstructExpr(has(ignoringImplicit(
-                        cxxMemberCallExpr(callee(cxxMethodDecl(hasName("end"))))
-                            .bind("end")))),
-                    anything())))
+              1, optionally(cxxMemberCallExpr(callee(cxxMethodDecl(hasName("end"))))
+                           .bind("end"))))
           .bind("alg");
 
   const auto DeclInStd = type(hasUnqualifiedDesugaredType(
       tagType(hasDeclaration(decl(isInStdNamespace())))));
   Finder->addMatcher(
-      traverse(
-          TK_AsIs,
-          cxxMemberCallExpr(
-              on(anyOf(hasType(DeclInStd), hasType(pointsTo(DeclInStd)))),
-              callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1),
-              hasArgument(0, has(ignoringImplicit(anyOf(
-                                 EndCall, has(ignoringImplicit(EndCall)))))),
-              unless(isInTemplateInstantiation()))
-              .bind("erase")),
+      cxxMemberCallExpr(
+          on(anyOf(hasType(DeclInStd), hasType(pointsTo(DeclInStd)))),
+          callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1),
+          hasArgument(0, EndCall))
+          .bind("erase"),
       this);
 }
 

diff  --git a/clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.h b/clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.h
index ef6006dca888..dd6eb80a41d1 100644
--- a/clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/InaccurateEraseCheck.h
@@ -31,6 +31,9 @@ class InaccurateEraseCheck : public ClangTidyCheck {
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  llvm::Optional<TraversalKind> getCheckTraversalKind() const override {
+    return TK_IgnoreUnlessSpelledInSource;
+  }
 };
 
 } // namespace bugprone

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-inaccurate-erase.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-inaccurate-erase.cpp
index eff57eaabf7a..d29fa9cdd4e9 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-inaccurate-erase.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-inaccurate-erase.cpp
@@ -1,5 +1,4 @@
-// RUN: %check_clang_tidy -std=c++11,c++14 %s bugprone-inaccurate-erase %t
-// FIXME: Fix the checker to work in C++17 mode.
+// RUN: %check_clang_tidy %s bugprone-inaccurate-erase %t
 
 namespace std {
 template <typename T> struct vec_iterator {


        


More information about the cfe-commits mailing list