Fixed #4180 (false positive: (style) Variable is assigned a value that is never used (inside loop))

This commit is contained in:
Frank Zingsheim 2012-11-28 06:11:33 +01:00 committed by Daniel Marjamäki
parent 4e92f8dfcd
commit f23ce8d254
2 changed files with 70 additions and 1 deletions

View File

@ -649,7 +649,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
// Check variable usage
for (const Token *tok = scope->classDef->next(); tok && tok != scope->classEnd; tok = tok->next()) {
if (tok->str() == "for" || tok->str() == "while") {
if (tok->str() == "for" || tok->str() == "while" || tok->str() == "do") {
for (std::list<Scope*>::const_iterator i = scope->nestedList.begin(); i != scope->nestedList.end(); ++i) {
if ((*i)->classDef == tok) { // Find associated scope
checkFunctionVariableUsage_iterateScopes(*i, variables, true, usedVariables); // Scan child scope

View File

@ -625,6 +625,18 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int i = 0,code=10,d=10;\n"
" for(i = 0; i < 10; i++) {\n"
" std::cout<<code<<std::endl;\n"
" code += 2;\n"
" g(d);\n"
" d = code;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int code=10;\n"
@ -646,6 +658,51 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int code=10,d=10;\n"
" while(code < 20) {\n"
" std::cout<<code<<std::endl;\n"
" code += 2;\n"
" g(d);\n"
" d += code;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int code=10;\n"
" do {\n"
" std::cout<<code<<std::endl;\n"
" code += 2;\n"
" } while(code < 20)\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int code=10,d=10;\n"
" do {\n"
" std::cout<<code<<std::endl;\n"
" code += 2;\n"
" d += code;\n"
" } while(code < 20)\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int code=10,d=10;\n"
" do {\n"
" std::cout<<code<<std::endl;\n"
" code += 2;\n"
" g(d);\n"
" d += code;\n"
" } while(code < 20)\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int code=10;\n"
@ -681,6 +738,18 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int code=10;\n"
" do {\n"
" if(true) {\n"
" std::cout<<code<<std::endl;\n"
" code += 2;\n"
" }\n"
" } while(code < 20)\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvar2() {