[clang-tools-extra] b2d8c89 - Remove false positive in AvoidNonConstGlobalVariables.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 15 11:48:18 PDT 2020


Author: Kim Viggedal
Date: 2020-04-15T14:48:06-04:00
New Revision: b2d8c89ea48beb83e0392b1f00c9eafa33c09ca8

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

LOG: Remove false positive in AvoidNonConstGlobalVariables.

Addresses post-commit review feedback from https://reviews.llvm.org/D70265

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
index da94ac03ae19..f8ae76d8456f 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp
@@ -17,11 +17,15 @@ namespace clang {
 namespace tidy {
 namespace cppcoreguidelines {
 
+namespace {
+AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
+} // namespace
+
 void AvoidNonConstGlobalVariablesCheck::registerMatchers(MatchFinder *Finder) {
   auto GlobalVariable = varDecl(
       hasGlobalStorage(),
       unless(anyOf(
-          isConstexpr(), hasType(isConstQualified()),
+          isLocalVarDecl(), isConstexpr(), hasType(isConstQualified()),
           hasType(referenceType())))); // References can't be changed, only the
                                        // data they reference can be changed.
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-non-const-global-variables.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-non-const-global-variables.cpp
index 1e554d6e59da..5b1c004837e7 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-non-const-global-variables.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-avoid-non-const-global-variables.cpp
@@ -231,7 +231,8 @@ constexpr T templateVariable = T(0L);
 // CHECKING AGAINST FALSE POSITIVES INSIDE FUNCTION SCOPE /////////////////////
 int main() {
   for (int i = 0; i < 3; ++i) {
+    static int staticNonConstLoopVariable = 42;
     int nonConstLoopVariable = 42;
-    nonConstInt = nonConstLoopVariable + i;
+    nonConstInt = nonConstLoopVariable + i + staticNonConstLoopVariable;
   }
 }


        


More information about the cfe-commits mailing list