[PATCH] D73843: [clang-tidy] Fix false positive for cppcoreguidelines-init-variables
Nathan James via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 1 15:26:17 PST 2020
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,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73843.241908.patch
Type: text/x-patch
Size: 1699 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200201/dfe2106a/attachment.bin>
More information about the cfe-commits
mailing list