[PATCH] D141838: [clang-tidy] fix a false positive of `cppcoreguidelines-avoid-non-const-global-variables`

Vincent Hong via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 16 03:53:38 PST 2023


v1nh1shungry created this revision.
v1nh1shungry added reviewers: carlosgalvezp, gribozavr2.
Herald added subscribers: shchenz, kbarton, xazax.hun, nemanjai.
Herald added a reviewer: njames93.
Herald added a project: All.
v1nh1shungry requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Currently this checker will report the non-const static
private/protected member of a class/struct.

Fixes https://github.com/llvm/llvm-project/issues/57919


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D141838

Files:
  clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/avoid-non-const-global-variables.cpp
@@ -228,6 +228,20 @@
 template <class T>
 constexpr T templateVariable = T(0L);
 
+// CHECKING FOR NON-CONST STATIC PRIVATE/PROTECTED CLASS MEMBERS //////////////
+class Foo {
+  static int nonConstStaticPrivateMember;
+protected:
+  static int nonConstStaticProtectedMember;
+};
+
+struct Bar {
+private:
+  static int nonConstStaticPrivateMember;
+protected:
+  static int nonConstStaticProtectedMember;
+};
+
 // CHECKING AGAINST FALSE POSITIVES INSIDE FUNCTION SCOPE /////////////////////
 int main() {
   for (int i = 0; i < 3; ++i) {
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
@@ -24,6 +24,7 @@
       hasGlobalStorage(),
       unless(anyOf(
           isLocalVarDecl(), isConstexpr(), hasType(isConstQualified()),
+          isPrivate(), isProtected(),
           hasType(referenceType())))); // References can't be changed, only the
                                        // data they reference can be changed.
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141838.489492.patch
Type: text/x-patch
Size: 1595 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230116/6823420a/attachment-0001.bin>


More information about the cfe-commits mailing list