Skip to content

Commit 766dd7b

Browse files
committed
[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
1 parent cb0a183 commit 766dd7b

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ using namespace clang::ast_matchers;
1515

1616
namespace clang::tidy::readability {
1717

18+
namespace {
19+
AST_MATCHER(CXXMethodDecl, isStatic) { return Node.isStatic(); }
20+
} // namespace
21+
1822
static unsigned getNameSpecifierNestingLevel(const QualType &QType) {
19-
if (const ElaboratedType *ElType = QType->getAs<ElaboratedType>()) {
23+
if (const auto *ElType = QType->getAs<ElaboratedType>()) {
2024
if (const NestedNameSpecifier *NestedSpecifiers = ElType->getQualifier()) {
2125
unsigned NameSpecifierNestingLevel = 1;
2226
do {
@@ -38,7 +42,7 @@ void StaticAccessedThroughInstanceCheck::storeOptions(
3842

3943
void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) {
4044
Finder->addMatcher(
41-
memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStaticStorageClass()),
45+
memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStatic()),
4246
varDecl(hasStaticStorageDuration()),
4347
enumConstantDecl())))
4448
.bind("memberExpression"),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ Changes in existing checks
219219
do-while loops into account for the `AllowIntegerConditions` and
220220
`AllowPointerConditions` options.
221221

222+
- Improved :doc:`readability-static-accessed-through-instance
223+
<clang-tidy/checks/readability/static-accessed-through-instance>` check to
224+
identify calls to static member functions with out-of-class inline definitions.
225+
222226
Removed checks
223227
^^^^^^^^^^^^^^
224228

clang-tools-extra/test/clang-tidy/checkers/readability/static-accessed-through-instance.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,20 @@ void testEmbeddedAnonymousStructAndClass() {
363363
}
364364

365365
} // namespace llvm_issue_61736
366+
367+
namespace PR51861 {
368+
class Foo {
369+
public:
370+
static Foo& getInstance();
371+
static int getBar();
372+
};
373+
374+
inline int Foo::getBar() { return 42; }
375+
376+
void test() {
377+
auto& params = Foo::getInstance();
378+
params.getBar();
379+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: static member accessed through instance [readability-static-accessed-through-instance]
380+
// CHECK-FIXES: {{^}} PR51861::Foo::getBar();{{$}}
381+
}
382+
}

0 commit comments

Comments
 (0)