From: Kim Viggedal Date: Wed, 15 Apr 2020 18:47:11 +0000 (-0400) Subject: Remove false positive in AvoidNonConstGlobalVariables. X-Git-Tag: llvmorg-12-init~8933 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b2d8c89ea48beb83e0392b1f00c9eafa33c09ca8;p=platform%2Fupstream%2Fllvm.git Remove false positive in AvoidNonConstGlobalVariables. Addresses post-commit review feedback from https://reviews.llvm.org/D70265 --- 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; } }