Fixed #9901 (False positive: variable is assigned value that is not used 'if (--N == 0)')

This commit is contained in:
Daniel Marjamäki 2020-09-26 22:13:05 +02:00
parent 0ec77879ea
commit 7bb82c5df7
2 changed files with 19 additions and 0 deletions

View File

@ -1132,6 +1132,10 @@ void CheckUnusedVar::checkFunctionVariableUsage()
const bool isIncrementOrDecrement = (tok->tokType() == Token::Type::eIncDecOp); const bool isIncrementOrDecrement = (tok->tokType() == Token::Type::eIncDecOp);
if (!isAssignment && !isInitialization && !isIncrementOrDecrement) if (!isAssignment && !isInitialization && !isIncrementOrDecrement)
continue; continue;
if (isIncrementOrDecrement && tok->astParent() && precedes(tok, tok->astOperand1()))
continue;
if (tok->isName()) { if (tok->isName()) {
if (mTokenizer->isCPP()) { if (mTokenizer->isCPP()) {
// do not check RAII/scope_lock objects // do not check RAII/scope_lock objects

View File

@ -117,6 +117,7 @@ private:
TEST_CASE(localvar55); TEST_CASE(localvar55);
TEST_CASE(localvar56); TEST_CASE(localvar56);
TEST_CASE(localvar57); // #8974 - increment TEST_CASE(localvar57); // #8974 - increment
TEST_CASE(localvar58); // #9901 - increment false positive
TEST_CASE(localvarloops); // loops TEST_CASE(localvarloops); // loops
TEST_CASE(localvaralias1); TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias2); // ticket #1637
@ -2270,6 +2271,20 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'x' is assigned a value that is never used.\n", errout.str());
} }
void localvar58() { // #9901 - increment false positive
functionVariableUsage("void f() {\n"
" int x = 0;\n"
" if (--x > 0) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void f() {\n"
" int x = 0;\n"
" if (x-- > 0) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (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"