[clang-tools-extra] efcd09c - [clang-tidy] Fix false positive for cppcoreguidelines-init-variables

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 2 13:26:36 PST 2020


Author: Nathan James
Date: 2020-02-02T21:26:29Z
New Revision: efcd09cea9a51c522954aa24e4b5513266daf6c3

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

LOG: [clang-tidy] Fix false positive for cppcoreguidelines-init-variables

Summary: Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=44746 | False positive for cppcoreguidelines-init-variables in range based for loop in template function ]]

Reviewers: aaron.ballman, alexfh, hokein, JonasToth, gribozavr2

Reviewed By: aaron.ballman

Subscribers: merge_guards_bot, xazax.hun, wuzish, nemanjai, kbarton, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D73843

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
index 8a628317a302..a5d9275afaf7 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -29,11 +29,15 @@ InitVariablesCheck::InitVariablesCheck(StringRef Name,
       MathHeader(Options.get("MathHeader", "math.h")) {}
 
 void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
-  Finder->addMatcher(varDecl(unless(hasInitializer(anything())),
-                             unless(isInstantiated()), isLocalVarDecl(),
-                             unless(isStaticLocal()), isDefinition())
-                         .bind("vardecl"),
-                     this);
+  std::string BadDecl = "badDecl";
+  Finder->addMatcher(
+      varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
+              isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
+              optionally(hasParent(declStmt(hasParent(
+                  cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
+              unless(equalsBoundNode(BadDecl)))
+          .bind("vardecl"),
+      this);
 }
 
 void InitVariablesCheck::registerPPCallbacks(const SourceManager &SM,

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
index d43e44808a49..4c92e1976f91 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
@@ -78,3 +78,9 @@ void init_unit_tests() {
   int parens(42);
   int braces{42};
 }
+
+template <typename RANGE>
+void f(RANGE r) {
+  for (char c : r) {
+  }
+}


        


More information about the cfe-commits mailing list