Remove false positive in AvoidNonConstGlobalVariables.
authorKim Viggedal <kimviggedal@gmail.com>
Wed, 15 Apr 2020 18:47:11 +0000 (14:47 -0400)
committerAaron Ballman <aaron@aaronballman.com>
Wed, 15 Apr 2020 18:48:06 +0000 (14:48 -0400)
Addresses post-commit review feedback from https://reviews.llvm.org/D70265

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

index da94ac0..f8ae76d 100644 (file)
@@ -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.
 
index 1e554d6..5b1c004 100644 (file)
@@ -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;
   }
 }