Index: clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp +++ clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp @@ -39,7 +39,7 @@ void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( - memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStaticStorageClass()), + memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStatic()), varDecl(hasStaticStorageDuration())))) .bind("memberExpression"), this); Index: clang-tools-extra/test/clang-tidy/checkers/readability-static-accessed-through-instance.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/readability-static-accessed-through-instance.cpp +++ clang-tools-extra/test/clang-tidy/checkers/readability-static-accessed-through-instance.cpp @@ -263,3 +263,20 @@ // CHECK-MESSAGES-NOT: :[[@LINE-1]]:10: warning: static member } // namespace Bugzilla_48758 + +// https://bugs.llvm.org/show_bug.cgi?id=52519 +struct PR52519 { + static PR52519 &getInstance(); + static int getInt(); +}; + +int PR52519::getInt() { + return 42; +} + +int PR52519_bar() { + auto &foo = PR52519::getInstance(); + return foo.getInt(); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: static member accessed through instance [readability-static-accessed-through-instance] + // CHECK-FIXES: {{^}} return PR52519::getInt();{{$}} +} Index: clang/docs/LibASTMatchersReference.html =================================================================== --- clang/docs/LibASTMatchersReference.html +++ clang/docs/LibASTMatchersReference.html @@ -3322,6 +3322,19 @@ +
Matches if the given method declaration is static.
+
+Given
+struct A {
+ static void foo();
+ void bar();
+};
+
+cxxMethodDecl(isStatic()) matches A::foo() but not A::bar()
+Matches if the given method declaration declares a copy assignment
operator.
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -6003,6 +6003,19 @@
return Node.isConst();
}
+/// Matches if the given method declaration is static.
+///
+/// Given
+/// \code
+/// struct A {
+/// static void foo();
+/// void bar();
+/// };
+/// \endcode
+///
+/// cxxMethodDecl(isStatic()) matches A::foo() but not A::bar()
+AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }
+
/// Matches if the given method declaration declares a copy assignment
/// operator.
///
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -451,6 +451,7 @@
REGISTER_MATCHER(isSharedKind);
REGISTER_MATCHER(isSignedInteger);
REGISTER_MATCHER(isStandaloneDirective);
+ REGISTER_MATCHER(isStatic);
REGISTER_MATCHER(isStaticLocal);
REGISTER_MATCHER(isStaticStorageClass);
REGISTER_MATCHER(isStruct);
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2017,6 +2017,17 @@
notMatches("struct A { void foo(); };", cxxMethodDecl(isConst())));
}
+TEST_P(ASTMatchersTest, IsStatic) {
+ if (!GetParam().isCXX()) {
+ return;
+ }
+
+ EXPECT_TRUE(
+ matches("struct A { static void foo(); };", cxxMethodDecl(isStatic())));
+ EXPECT_TRUE(
+ notMatches("struct A { void foo(); };", cxxMethodDecl(isStatic())));
+}
+
TEST_P(ASTMatchersTest, IsOverride) {
if (!GetParam().isCXX()) {
return;