diff --git a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp @@ -15,8 +15,12 @@ namespace clang::tidy::readability { +namespace { +AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); } +} // namespace + static unsigned getNameSpecifierNestingLevel(const QualType &QType) { - if (const ElaboratedType *ElType = QType->getAs()) { + if (const auto *ElType = QType->getAs()) { if (const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier()) { unsigned NameSpecifierNestingLevel = 1; do { @@ -38,7 +42,7 @@ void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( - memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStaticStorageClass()), + memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStatic()), varDecl(hasStaticStorageDuration()), enumConstantDecl()))) .bind("memberExpression"), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -219,6 +219,10 @@ do-while loops into account for the `AllowIntegerConditions` and `AllowPointerConditions` options. +- Improved :doc:`readability-static-accessed-through-instance + ` check to + identify calls to static member functions with out-of-class inline definitions. + Removed checks ^^^^^^^^^^^^^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp @@ -363,3 +363,20 @@ } } // namespace llvm_issue_61736 + +namespace PR51861 { + class Foo { + public: + static Foo& getInstance(); + static int getBar(); + }; + + inline int Foo::getBar() { return 42; } + + void test() { + auto& params = Foo::getInstance(); + params.getBar(); + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: static member accessed through instance [readability-static-accessed-through-instance] + // CHECK-FIXES: {{^}} PR51861::Foo::getBar();{{$}} + } +}