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 @@ -39,7 +39,8 @@ void StaticAccessedThroughInstanceCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( memberExpr(hasDeclaration(anyOf(cxxMethodDecl(isStaticStorageClass()), - varDecl(hasStaticStorageDuration())))) + varDecl(hasStaticStorageDuration()), + enumConstantDecl()))) .bind("memberExpression"), this); } @@ -64,15 +65,15 @@ : BaseExpr->getType().getUnqualifiedType(); const ASTContext *AstContext = Result.Context; - PrintingPolicy PrintingPolicyWithSupressedTag(AstContext->getLangOpts()); - PrintingPolicyWithSupressedTag.SuppressTagKeyword = true; - PrintingPolicyWithSupressedTag.SuppressUnwrittenScope = true; + PrintingPolicy PrintingPolicyWithSuppressedTag(AstContext->getLangOpts()); + PrintingPolicyWithSuppressedTag.SuppressTagKeyword = true; + PrintingPolicyWithSuppressedTag.SuppressUnwrittenScope = true; - PrintingPolicyWithSupressedTag.PrintCanonicalTypes = + PrintingPolicyWithSuppressedTag.PrintCanonicalTypes = !BaseExpr->getType()->isTypedefNameType(); std::string BaseTypeName = - BaseType.getAsString(PrintingPolicyWithSupressedTag); + BaseType.getAsString(PrintingPolicyWithSuppressedTag); // Do not warn for CUDA built-in variables. if (StringRef(BaseTypeName).startswith("__cuda_builtin_")) 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 @@ -254,6 +254,10 @@ ` check when warning would be unnecessarily emitted for template dependent ``if constexpr``. +- Improved :doc:`readability-static-accessed-through-instance + ` check to + support unscoped enumerations through instances. + - Fixed a false positive in :doc:`cppcoreguidelines-slicing ` check when warning would be emitted in constructor for virtual base class initialization. diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/static-accessed-through-instance.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/static-accessed-through-instance.rst --- a/clang-tools-extra/docs/clang-tidy/checks/readability/static-accessed-through-instance.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/readability/static-accessed-through-instance.rst @@ -15,11 +15,15 @@ struct C { static void foo(); static int x; + enum { E1 }; + enum E { E2 }; }; C *c1 = new C(); c1->foo(); c1->x; + c1->E1; + c1->E2; is changed to: @@ -28,4 +32,6 @@ C *c1 = new C(); C::foo(); C::x; + C::E1; + C::E2; 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 @@ -1,10 +1,21 @@ // RUN: %check_clang_tidy %s readability-static-accessed-through-instance %t -- -- -isystem %S/Inputs/static-accessed-through-instance #include <__clang_cuda_builtin_vars.h> +enum OutEnum { + E0, +}; + struct C { static void foo(); static int x; int nsx; + enum { + Anonymous, + }; + enum E { + E1, + }; + using enum OutEnum; void mf() { (void)&x; // OK, x is accessed inside the struct. (void)&C::x; // OK, x is accessed using a qualified-id. @@ -144,6 +155,16 @@ c1->x; // 2 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member // CHECK-FIXES: {{^}} C::x; // 2{{$}} + c1->Anonymous; // 3 + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member + // CHECK-FIXES: {{^}} C::Anonymous; // 3{{$}} + c1->E1; // 4 + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member + // CHECK-FIXES: {{^}} C::E1; // 4{{$}} + c1->E0; // 5 + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: static member + // CHECK-FIXES: {{^}} C::E0; // 5{{$}} + c1->nsx; // OK, nsx is a non-static member. const C *c2 = new C();