[clang-tidy][NFC] Simply match processing in misc-use-anonymous-namespace
authorCarlos Galvez <carlosgalvezp@gmail.com>
Mon, 12 Dec 2022 13:36:29 +0000 (13:36 +0000)
committerCarlos Galvez <carlosgalvezp@gmail.com>
Mon, 12 Dec 2022 14:05:19 +0000 (14:05 +0000)
No need for the templated function "processMatch", since
we can infer the type with llvm:isa.

clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp
clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h

index 878cba3..fdf7828 100644 (file)
@@ -31,33 +31,26 @@ AST_MATCHER(Decl, isInAnonymousNamespace) {
 }
 } // namespace
 
-template <typename T>
-void UseAnonymousNamespaceCheck::processMatch(const T *MatchedDecl) {
-  StringRef Type = llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function";
-  diag(MatchedDecl->getLocation(),
-       "%0 %1 declared 'static', move to anonymous namespace instead")
-      << Type << MatchedDecl;
-}
-
 void UseAnonymousNamespaceCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       functionDecl(isStatic(),
                    unless(anyOf(isInAnonymousNamespace(), isMemberFunction())))
-          .bind("func"),
+          .bind("x"),
       this);
   Finder->addMatcher(
       varDecl(isStatic(), unless(anyOf(isInAnonymousNamespace(),
                                        isStaticLocal(), isStaticDataMember())))
-          .bind("var"),
+          .bind("x"),
       this);
 }
 
 void UseAnonymousNamespaceCheck::check(const MatchFinder::MatchResult &Result) {
-  if (const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("func"))
-    processMatch(MatchedDecl);
-
-  if (const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("var"))
-    processMatch(MatchedDecl);
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs<NamedDecl>("x")) {
+    StringRef Type = llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function";
+    diag(MatchedDecl->getLocation(),
+         "%0 %1 declared 'static', move to anonymous namespace instead")
+        << Type << MatchedDecl;
+  }
 }
 
 } // namespace misc
index 59c4802..19d76bf 100644 (file)
@@ -29,9 +29,6 @@ public:
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
-
-private:
-  template <typename T> void processMatch(const T *MatchedDecl);
 };
 
 } // namespace misc