From fd67ca146d6be0f93da4e330d85f9f6360d7ef6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 30 Jan 2016 16:49:39 +0100 Subject: [PATCH] Fixed #4955 (false positive: Variable 'i' is assigned a value that is never used (only used in template instantiation)) --- lib/checkunusedvar.cpp | 4 ++++ test/testunusedvar.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index c764f297e..870b6e1d4 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -769,6 +769,10 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const variables.clear(); break; } + if (tok->isName() && tok->str().back() == '>') { + variables.clear(); + break; + } // bailout when for_each is used if (Token::Match(tok, "%name% (") && Token::simpleMatch(tok->linkAt(1), ") {") && !Token::Match(tok, "if|for|while|switch")) { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 1f86c08de..27017a531 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -156,6 +156,7 @@ private: TEST_CASE(localvarUnusedGoto); // #4447, #4558 goto TEST_CASE(localvarRangeBasedFor); // #7075 TEST_CASE(localvarAssignInWhile); + TEST_CASE(localvarTemplate); // #4955 - variable is used as template parameter TEST_CASE(localvarCppInitialization); TEST_CASE(localvarCpp11Initialization); @@ -3875,6 +3876,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarTemplate() { + functionVariableUsage("template void f() {}\n" + "void foo() {\n" + " const int x = 0;\n" + " f();\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void chainedAssignment() { // #5466 functionVariableUsage("void NotUsed(double* pdD, int n) {\n"