diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index e033b04ad..ca581a1f0 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1132,6 +1132,10 @@ void CheckUnusedVar::checkFunctionVariableUsage() const bool isIncrementOrDecrement = (tok->tokType() == Token::Type::eIncDecOp); if (!isAssignment && !isInitialization && !isIncrementOrDecrement) continue; + + if (isIncrementOrDecrement && tok->astParent() && precedes(tok, tok->astOperand1())) + continue; + if (tok->isName()) { if (mTokenizer->isCPP()) { // do not check RAII/scope_lock objects diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 4d91daac2..3fdfb59cd 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -117,6 +117,7 @@ private: TEST_CASE(localvar55); TEST_CASE(localvar56); TEST_CASE(localvar57); // #8974 - increment + TEST_CASE(localvar58); // #9901 - increment false positive TEST_CASE(localvarloops); // loops TEST_CASE(localvaralias1); 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()); } + 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() { // loops functionVariableUsage("void fun() {\n"