diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8de58d6a7..c873bf258 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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("="); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d93cd0642..2e7245459 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -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() { { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index e5ed6a56d..0d3384f6d 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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"