Skip to content

Commit efcd09c

Browse files
committed
[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
1 parent ee85415 commit efcd09c

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ InitVariablesCheck::InitVariablesCheck(StringRef Name,
2929
MathHeader(Options.get("MathHeader", "math.h")) {}
3030

3131
void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
32-
Finder->addMatcher(varDecl(unless(hasInitializer(anything())),
33-
unless(isInstantiated()), isLocalVarDecl(),
34-
unless(isStaticLocal()), isDefinition())
35-
.bind("vardecl"),
36-
this);
32+
std::string BadDecl = "badDecl";
33+
Finder->addMatcher(
34+
varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
35+
isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
36+
optionally(hasParent(declStmt(hasParent(
37+
cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
38+
unless(equalsBoundNode(BadDecl)))
39+
.bind("vardecl"),
40+
this);
3741
}
3842

3943
void InitVariablesCheck::registerPPCallbacks(const SourceManager &SM,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,9 @@ void init_unit_tests() {
7878
int parens(42);
7979
int braces{42};
8080
}
81+
82+
template <typename RANGE>
83+
void f(RANGE r) {
84+
for (char c : r) {
85+
}
86+
}

0 commit comments

Comments
 (0)