From b2d8c89ea48beb83e0392b1f00c9eafa33c09ca8 Mon Sep 17 00:00:00 2001 From: Kim Viggedal Date: Wed, 15 Apr 2020 14:47:11 -0400 Subject: [PATCH] Remove false positive in AvoidNonConstGlobalVariables. Addresses post-commit review feedback from https://reviews.llvm.org/D70265 --- .../cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp | 6 +++++- .../checkers/cppcoreguidelines-avoid-non-const-global-variables.cpp | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/AvoidNonConstGlobalVariablesCheck.cpp index da94ac0..f8ae76d 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 1e554d6..5b1c0048 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; } } -- 2.7.4