[llvm-branch-commits] [clang-tools-extra] 84cda4c - [clang-tidy] Fix false positive for cppcoreguidelines-init-variables

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Feb 10 05:19:17 PST 2020


Author: Nathan James
Date: 2020-02-10T14:17:54+01:00
New Revision: 84cda4cceabdfec4f130bfafe7bbd050aa65b2ec

URL: https://github.com/llvm/llvm-project/commit/84cda4cceabdfec4f130bfafe7bbd050aa65b2ec
DIFF: https://github.com/llvm/llvm-project/commit/84cda4cceabdfec4f130bfafe7bbd050aa65b2ec.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

(cherry picked from commit efcd09cea9a51c522954aa24e4b5513266daf6c3)

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 llvm-branch-commits mailing list