unused var; fix todo test cases, redundant assignment of array in loop

This commit is contained in:
Daniel Marjamäki 2021-05-14 19:07:30 +02:00
parent 56410c3d00
commit 404b82fc21
2 changed files with 13 additions and 6 deletions

View File

@ -2784,6 +2784,8 @@ std::set<nonneg int> FwdAnalysis::getExprVarIds(const Token* expr, bool* localOu
bool unknownVarId = false;
visitAstNodes(expr,
[&](const Token *tok) {
if (tok->str() == "[" && mWhat == What::UnusedValue)
return ChildrenToVisit::op1;
if (tok->varId() == 0 && tok->isName() && tok->previous()->str() != ".") {
// unknown variable
unknownVarId = true;

View File

@ -2177,22 +2177,27 @@ private:
}
void localvar6() {
functionVariableUsage("void foo()\n"
"{\n"
functionVariableUsage("void foo() {\n"
" int b[10];\n"
" for (int i=0;i<10;++i)\n"
" b[i] = 0;\n"
"}");
// TODO ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'b' is assigned a value that is never used.\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b[i]' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
functionVariableUsage("void foo() {\n"
" int a = 0;\n"
" int b[10];\n"
" for (int i=0;i<10;++i)\n"
" b[i] = ++a;\n"
"}");
// TODO ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'b' is assigned a value that is never used.\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'b[i]' is assigned a value that is never used.\n", errout.str());
functionVariableUsage("void foo() {\n"
" int b[10];\n"
" for (int i=0;i<10;++i)\n"
" *(b+i) = 0;\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Variable '*(b+i)' is assigned a value that is never used.\n", "", errout.str());
}
void localvar8() {