Fixed #8974 (false negative regression about never used variable)

This commit is contained in:
Daniel Marjamäki 2019-07-29 08:49:19 +02:00
parent 602911cfca
commit a1214348c0
2 changed files with 15 additions and 30 deletions

View File

@ -1118,8 +1118,11 @@ void CheckUnusedVar::checkFunctionVariableUsage()
tok = eq;
}
}
// not assignment/initialization => continue
if ((!tok->isAssignmentOp() || !tok->astOperand1()) && !(Token::Match(tok, "%var% (") && tok->variable() && tok->variable()->nameToken() == tok))
// not assignment/initialization/increment => continue
const bool isAssignment = tok->isAssignmentOp() && tok->astOperand1();
const bool isInitialization = (Token::Match(tok, "%var% (") && tok->variable() && tok->variable()->nameToken() == tok);
const bool isIncrementOrDecrement = (tok->tokType() == Token::Type::eIncDecOp);
if (!isAssignment && !isInitialization && !isIncrementOrDecrement)
continue;
if (tok->isName()) {
if (mTokenizer->isCPP()) {

View File

@ -61,7 +61,6 @@ private:
TEST_CASE(localvar4);
TEST_CASE(localvar5);
TEST_CASE(localvar6);
TEST_CASE(localvar7);
TEST_CASE(localvar8);
TEST_CASE(localvar9); // ticket #1605
TEST_CASE(localvar10);
@ -111,6 +110,7 @@ private:
TEST_CASE(localvar54); // ast, {}
TEST_CASE(localvar55);
TEST_CASE(localvar56);
TEST_CASE(localvar57); // #8974 - increment
TEST_CASE(localvarloops); // loops
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
@ -1162,33 +1162,6 @@ private:
// TODO ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'b' is assigned a value that is never used.\n", errout.str());
}
void localvar7() { // ticket 1253
functionVariableUsage("void foo()\n"
"{\n"
" int i;\n"
" i--;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is not assigned a value.\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int i;\n"
" int &ii(i);\n"
" ii--;\n"
"}");
// TODO ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is not assigned a value.\n"
// TODO "[test.cpp:5]: (style) Variable 'ii' is modified but its new value is never used.\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int i;\n"
" int &ii=i;\n"
" ii--;\n"
"}");
// TODO ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is not assigned a value.\n"
// TODO "[test.cpp:5]: (style) Variable 'ii' is modified but its new value is never used.\n", errout.str());
}
void localvar8() {
functionVariableUsage("void foo()\n"
"{\n"
@ -2158,6 +2131,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvar57() {
functionVariableUsage("void f()\n"
"{\n"
" int x = 0;\n"
" x++;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str());
}
void localvarloops() {
// loops
functionVariableUsage("void fun() {\n"