Skip to content

Commit fd67ca1

Browse files
committed
Fixed #4955 (false positive: Variable 'i' is assigned a value that is never used (only used in template instantiation))
1 parent 0e89620 commit fd67ca1

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
769769
variables.clear();
770770
break;
771771
}
772+
if (tok->isName() && tok->str().back() == '>') {
773+
variables.clear();
774+
break;
775+
}
772776

773777
// bailout when for_each is used
774778
if (Token::Match(tok, "%name% (") && Token::simpleMatch(tok->linkAt(1), ") {") && !Token::Match(tok, "if|for|while|switch")) {

test/testunusedvar.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class TestUnusedVar : public TestFixture {
156156
TEST_CASE(localvarUnusedGoto); // #4447, #4558 goto
157157
TEST_CASE(localvarRangeBasedFor); // #7075
158158
TEST_CASE(localvarAssignInWhile);
159+
TEST_CASE(localvarTemplate); // #4955 - variable is used as template parameter
159160

160161
TEST_CASE(localvarCppInitialization);
161162
TEST_CASE(localvarCpp11Initialization);
@@ -3875,6 +3876,15 @@ class TestUnusedVar : public TestFixture {
38753876
ASSERT_EQUALS("", errout.str());
38763877
}
38773878

3879+
void localvarTemplate() {
3880+
functionVariableUsage("template<int A> void f() {}\n"
3881+
"void foo() {\n"
3882+
" const int x = 0;\n"
3883+
" f<x>();\n"
3884+
"}");
3885+
ASSERT_EQUALS("", errout.str());
3886+
}
3887+
38783888
void chainedAssignment() {
38793889
// #5466
38803890
functionVariableUsage("void NotUsed(double* pdD, int n) {\n"

0 commit comments

Comments
 (0)