[clang-tools-extra] 766dd7b - [clang-tidy] Fix handling of out-of-line functions in readability-static-accessed-through-instance

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 14 23:59:18 PDT 2023


Author: Piotr Zegar
Date: 2023-08-15T06:59:11Z
New Revision: 766dd7b80b575e723c8cb454532b614f9d5f2bc7

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

LOG: [clang-tidy] Fix handling of out-of-line functions in readability-static-accessed-through-instance

Use isStatic instead of isStaticStorageClass to properly
handle a out-of-line definitions.

Fixes: #51861

Reviewed By: carlosgalvezp

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
index 619374cddbcf6d..93c31d80c11381 100644
--- a/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp
@@ -15,8 +15,12 @@ using namespace clang::ast_matchers;
 
 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<ElaboratedType>()) {
+  if (const auto *ElType = QType->getAs<ElaboratedType>()) {
     if (const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier()) {
       unsigned NameSpecifierNestingLevel = 1;
       do {
@@ -38,7 +42,7 @@ void StaticAccessedThroughInstanceCheck::storeOptions(
 
 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
index 46cda6a8caf1f5..d3bb5b9ae69f61 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -219,6 +219,10 @@ Changes in existing checks
   do-while loops into account for the `AllowIntegerConditions` and
   `AllowPointerConditions` options.
 
+- Improved :doc:`readability-static-accessed-through-instance
+  <clang-tidy/checks/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
index 9e8930084a65a1..81c1cecf607f66 100644
--- 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 @@ void testEmbeddedAnonymousStructAndClass() {
 }
 
 } // 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();{{$}}
+  }
+}


        


More information about the cfe-commits mailing list