Fixed #1989 (false positive: Variable 'Aux13' is assigned a value that is never used)

This commit is contained in:
Daniel Marjamäki 2010-08-28 13:32:43 +02:00
parent b1bf201a8a
commit 71453871d4
3 changed files with 23 additions and 5 deletions

View File

@ -2020,7 +2020,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::s
// "if (p or q)" => "if (p || q)"
while (simplifyLogicalOperators()) { }
//updateClassList();
// Change initialisation of variable to assignment
simplifyInitVar();
setVarId();
// Change initialisation of variable to assignment
@ -5571,6 +5573,7 @@ Token * Tokenizer::initVar(Token * tok)
// insert '; var ='
tok->insertToken(";");
tok->next()->insertToken(tok->str());
tok->tokAt(2)->varId(tok->varId());
tok = tok->tokAt(2);
tok->insertToken("=");

View File

@ -46,6 +46,7 @@ private:
TEST_CASE(tokenize5);
TEST_CASE(tokenize6);
TEST_CASE(tokenize7);
TEST_CASE(tokenize8);
// array access. replace "*(p+1)" => "p[1]"
TEST_CASE(tokenize6);
@ -381,6 +382,20 @@ private:
tokenizeAndStringify(code.c_str(), false));
}
void tokenize8()
{
const std::string code = "void f() {\n"
" int x1(g());\n"
" int x2(x1);\n"
"}\n";
ASSERT_EQUALS("\n\n##file 0\n"
"1: void f ( ) {\n"
"2: int x1@1 ; x1@1 = g ( ) ;\n"
"3: int x2@2 ; x2@2 = x1@1 ;\n"
"4: }\n",
tokenizeDebugListing(code.c_str(), false));
}
void wrong_syntax()
{
{

View File

@ -349,7 +349,7 @@ private:
"{\n"
" int i(0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: i\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
// if a is undefined then Cppcheck can't determine if "int i(a)" is a
// * variable declaration
@ -365,7 +365,7 @@ private:
" int j = 0;\n"
" int i(j);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: i\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -404,14 +404,14 @@ private:
" int * j = 0;\n"
" int * i(j);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: i\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int * j = 0;\n"
" const int * i(j);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: i\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"