Fixed #8974 (false negative regression about never used variable)
This commit is contained in:
parent
602911cfca
commit
a1214348c0
|
@ -1118,8 +1118,11 @@ void CheckUnusedVar::checkFunctionVariableUsage()
|
||||||
tok = eq;
|
tok = eq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// not assignment/initialization => continue
|
// not assignment/initialization/increment => continue
|
||||||
if ((!tok->isAssignmentOp() || !tok->astOperand1()) && !(Token::Match(tok, "%var% (") && tok->variable() && tok->variable()->nameToken() == tok))
|
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;
|
continue;
|
||||||
if (tok->isName()) {
|
if (tok->isName()) {
|
||||||
if (mTokenizer->isCPP()) {
|
if (mTokenizer->isCPP()) {
|
||||||
|
|
|
@ -61,7 +61,6 @@ private:
|
||||||
TEST_CASE(localvar4);
|
TEST_CASE(localvar4);
|
||||||
TEST_CASE(localvar5);
|
TEST_CASE(localvar5);
|
||||||
TEST_CASE(localvar6);
|
TEST_CASE(localvar6);
|
||||||
TEST_CASE(localvar7);
|
|
||||||
TEST_CASE(localvar8);
|
TEST_CASE(localvar8);
|
||||||
TEST_CASE(localvar9); // ticket #1605
|
TEST_CASE(localvar9); // ticket #1605
|
||||||
TEST_CASE(localvar10);
|
TEST_CASE(localvar10);
|
||||||
|
@ -111,6 +110,7 @@ private:
|
||||||
TEST_CASE(localvar54); // ast, {}
|
TEST_CASE(localvar54); // ast, {}
|
||||||
TEST_CASE(localvar55);
|
TEST_CASE(localvar55);
|
||||||
TEST_CASE(localvar56);
|
TEST_CASE(localvar56);
|
||||||
|
TEST_CASE(localvar57); // #8974 - increment
|
||||||
TEST_CASE(localvarloops); // loops
|
TEST_CASE(localvarloops); // loops
|
||||||
TEST_CASE(localvaralias1);
|
TEST_CASE(localvaralias1);
|
||||||
TEST_CASE(localvaralias2); // ticket #1637
|
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());
|
// 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() {
|
void localvar8() {
|
||||||
functionVariableUsage("void foo()\n"
|
functionVariableUsage("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -2158,6 +2131,15 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
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() {
|
void localvarloops() {
|
||||||
// loops
|
// loops
|
||||||
functionVariableUsage("void fun() {\n"
|
functionVariableUsage("void fun() {\n"
|
||||||
|
|
Loading…
Reference in New Issue