From ea0cbbcf785cb75a88828accc5bbabfea8603ede Mon Sep 17 00:00:00 2001 From: Kartik Bajaj Date: Sun, 2 Sep 2012 18:50:17 +0200 Subject: [PATCH] Fixed #1481 --- lib/checkunusedvar.cpp | 9 +++++++-- test/testunusedvar.cpp | 33 ++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 0aa02c377..800244dcf 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -256,8 +256,10 @@ void Variables::write(unsigned int varid) { VariableUsage *usage = find(varid); - if (usage) + if (usage) { usage->_write = true; + usage->_read = false; + } } void Variables::writeAliases(unsigned int varid) @@ -730,9 +732,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } } + else if (Token::Match(tok->tokAt(-2), "while|if") && Token::Match(tok->tokAt(1), "=") && tok->varId() && tok->varId() == tok->tokAt(2)->varId()) { + variables.use(tok->tokAt(2)->varId()); + } // assignment else if (!Token::Match(tok->tokAt(-2), "[;{}.] %var% (") && - (Token::Match(tok, "*| (| ++|--| %var% ++|--| )| =") || + (Token::Match(tok, "*| ++|--| %var% ++|--| =") || Token::Match(tok, "*| ( const| %type% *| ) %var% ="))) { bool dereference = false; bool pre = false; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 668338fc9..1a2339741 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -424,23 +424,23 @@ private: "{\n" " int j = 0;\n" " int & i = j;\n" - " j = j;\n" + " x(j);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str()); + ASSERT_EQUALS("", errout.str()); functionVariableUsage("void foo()\n" "{\n" " int j = 0;\n" " const int & i = j;\n" - " j = j;\n" + " x(j);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str()); + ASSERT_EQUALS("", errout.str()); functionVariableUsage("void foo()\n" "{\n" " int j = 0;\n" " int & i(j);\n" - " j = j;\n" + " x(j);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str()); @@ -448,7 +448,7 @@ private: "{\n" " int j = 0;\n" " const int & i(j);\n" - " j = j;\n" + " x(j);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str()); @@ -583,6 +583,12 @@ private: " char *i = \"123456789\";\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str()); + + functionVariableUsage("void foo()\n" + "{\n" + " int i = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str()); } void localvar2() { @@ -975,7 +981,9 @@ private: " int a, b, c;\n" " a = b = c = f();\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n" + "[test.cpp:3]: (style) Variable 'b' is assigned a value that is never used\n" + "[test.cpp:3]: (style) Variable 'c' is assigned a value that is never used\n", errout.str()); functionVariableUsage("int * foo()\n" "{\n" @@ -1184,7 +1192,7 @@ private: " char *ptr = buf;\n" " *(ptr++) = 0;\n" "}\n"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str()); functionVariableUsage("int foo()\n" "{\n" @@ -1192,7 +1200,7 @@ private: " char *ptr = buf - 1;\n" " *(++ptr) = 0;\n" "}\n"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'buf' is not assigned a value\n", errout.str()); // #3910 functionVariableUsage("int foo() {\n" @@ -1516,7 +1524,6 @@ private: " piArray[uiIndex] = -1234;\n" " }\n" " delete [] piArray;\n" - " piArray = NULL;\n" "}\n"); ASSERT_EQUALS("", errout.str()); } @@ -1757,7 +1764,7 @@ private: "{\n" " int *b = a;\n" " int c = b[0];\n" - " c = c;\n" + " x(c);\n" "}\n"); ASSERT_EQUALS("", errout.str()); @@ -1765,7 +1772,7 @@ private: "{\n" " int *b = a;\n" " int c = b[0];\n" - " c = c;\n" + " x(c);\n" "}\n"); ASSERT_EQUALS("", errout.str()); @@ -1980,7 +1987,7 @@ private: functionVariableUsage("void foo()\n" "{\n" " int * a;\n" - " a = a;\n" + " x(a);\n" "}\n"); ASSERT_EQUALS("", errout.str()); }