njames93 created this revision.
Herald added subscribers: cfe-commits, kbarton, nemanjai.
Herald added a project: clang.
njames93 retitled this revision from "[clang-tidt] Fix false positive for
cppcoreguidelines-init-variables" to "[clang-tidy] Fix false positive for
cppcoreguidelines-init-variables".
njames93 edited the summary of this revision.
njames93 added reviewers: aaron.ballman, alexfh, hokein, JonasToth, gribozavr2.
njames93 added a project: clang-tools-extra.
Herald added subscribers: wuzish, xazax.hun.
njames93 marked an inline comment as done.
njames93 added inline comments.
================
Comment at:
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp:37
+ optionally(hasParent(declStmt(hasParent(
+
cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
+ unless(equalsBoundNode(BadDecl)))
----------------
No point checking if its a template definition or not. According to the
standard it's not possible to have a for range statement where the loop
variable isn't initialized.
Fixes False positive for cppcoreguidelines-init-variables in range based for
loop in template function <https://bugs.llvm.org/show_bug.cgi?id=44746>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D73843
Files:
clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
Index:
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
===================================================================
---
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
+++
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
@@ -78,3 +78,9 @@
int parens(42);
int braces{42};
}
+
+template <typename RANGE>
+void f(RANGE r) {
+ for (char c : r) {
+ }
+}
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -29,11 +29,15 @@
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,
Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-init-variables.cpp
@@ -78,3 +78,9 @@
int parens(42);
int braces{42};
}
+
+template <typename RANGE>
+void f(RANGE r) {
+ for (char c : r) {
+ }
+}
Index: clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
+++ clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
@@ -29,11 +29,15 @@
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,
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits