Fixed #1481
This commit is contained in:
parent
c9b2174dea
commit
ea0cbbcf78
|
@ -256,8 +256,10 @@ void Variables::write(unsigned int varid)
|
||||||
{
|
{
|
||||||
VariableUsage *usage = find(varid);
|
VariableUsage *usage = find(varid);
|
||||||
|
|
||||||
if (usage)
|
if (usage) {
|
||||||
usage->_write = true;
|
usage->_write = true;
|
||||||
|
usage->_read = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Variables::writeAliases(unsigned int varid)
|
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
|
// assignment
|
||||||
else if (!Token::Match(tok->tokAt(-2), "[;{}.] %var% (") &&
|
else if (!Token::Match(tok->tokAt(-2), "[;{}.] %var% (") &&
|
||||||
(Token::Match(tok, "*| (| ++|--| %var% ++|--| )| =") ||
|
(Token::Match(tok, "*| ++|--| %var% ++|--| =") ||
|
||||||
Token::Match(tok, "*| ( const| %type% *| ) %var% ="))) {
|
Token::Match(tok, "*| ( const| %type% *| ) %var% ="))) {
|
||||||
bool dereference = false;
|
bool dereference = false;
|
||||||
bool pre = false;
|
bool pre = false;
|
||||||
|
|
|
@ -424,23 +424,23 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" int j = 0;\n"
|
" int j = 0;\n"
|
||||||
" int & i = j;\n"
|
" int & i = j;\n"
|
||||||
" j = j;\n"
|
" x(j);\n"
|
||||||
"}\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"
|
functionVariableUsage("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int j = 0;\n"
|
" int j = 0;\n"
|
||||||
" const int & i = j;\n"
|
" const int & i = j;\n"
|
||||||
" j = j;\n"
|
" x(j);\n"
|
||||||
"}\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"
|
functionVariableUsage("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int j = 0;\n"
|
" int j = 0;\n"
|
||||||
" int & i(j);\n"
|
" int & i(j);\n"
|
||||||
" j = j;\n"
|
" x(j);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
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"
|
"{\n"
|
||||||
" int j = 0;\n"
|
" int j = 0;\n"
|
||||||
" const int & i(j);\n"
|
" const int & i(j);\n"
|
||||||
" j = j;\n"
|
" x(j);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
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"
|
" char *i = \"123456789\";\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
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() {
|
void localvar2() {
|
||||||
|
@ -975,7 +981,9 @@ private:
|
||||||
" int a, b, c;\n"
|
" int a, b, c;\n"
|
||||||
" a = b = c = f();\n"
|
" a = b = c = f();\n"
|
||||||
"}\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"
|
functionVariableUsage("int * foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1184,7 +1192,7 @@ private:
|
||||||
" char *ptr = buf;\n"
|
" char *ptr = buf;\n"
|
||||||
" *(ptr++) = 0;\n"
|
" *(ptr++) = 0;\n"
|
||||||
"}\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"
|
functionVariableUsage("int foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -1192,7 +1200,7 @@ private:
|
||||||
" char *ptr = buf - 1;\n"
|
" char *ptr = buf - 1;\n"
|
||||||
" *(++ptr) = 0;\n"
|
" *(++ptr) = 0;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'buf' is not assigned a value\n", errout.str());
|
||||||
|
|
||||||
// #3910
|
// #3910
|
||||||
functionVariableUsage("int foo() {\n"
|
functionVariableUsage("int foo() {\n"
|
||||||
|
@ -1516,7 +1524,6 @@ private:
|
||||||
" piArray[uiIndex] = -1234;\n"
|
" piArray[uiIndex] = -1234;\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
" delete [] piArray;\n"
|
" delete [] piArray;\n"
|
||||||
" piArray = NULL;\n"
|
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
@ -1757,7 +1764,7 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" int *b = a;\n"
|
" int *b = a;\n"
|
||||||
" int c = b[0];\n"
|
" int c = b[0];\n"
|
||||||
" c = c;\n"
|
" x(c);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
@ -1765,7 +1772,7 @@ private:
|
||||||
"{\n"
|
"{\n"
|
||||||
" int *b = a;\n"
|
" int *b = a;\n"
|
||||||
" int c = b[0];\n"
|
" int c = b[0];\n"
|
||||||
" c = c;\n"
|
" x(c);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
@ -1980,7 +1987,7 @@ private:
|
||||||
functionVariableUsage("void foo()\n"
|
functionVariableUsage("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int * a;\n"
|
" int * a;\n"
|
||||||
" a = a;\n"
|
" x(a);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue