diff --git a/lib/checkother.cpp b/lib/checkother.cpp index bae7ea263..0f9378680 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -667,7 +667,7 @@ void CheckOther::checkRedundantAssignment() if (!Token::simpleMatch(tok->tokAt(2), "0 ;") || (tok->variable() && tok->variable()->nameToken() != tok->tokAt(-2))) varAssignments[tok->varId()] = tok; memAssignments.erase(tok->varId()); - } else if (tok->next()->type() == Token::eIncDecOp || (tok->previous()->type() == Token::eIncDecOp && !Token::Match(tok->next(), ".|[|("))) { // Variable incremented/decremented + } else if (tok->next()->type() == Token::eIncDecOp || (tok->previous()->type() == Token::eIncDecOp && tok->strAt(1) == ";")) { // Variable incremented/decremented; Prefix-Increment is only suspicious, if its return value is unused varAssignments[tok->varId()] = tok; memAssignments.erase(tok->varId()); } else if (!Token::simpleMatch(tok->tokAt(-2), "sizeof (")) { // Other usage of variable diff --git a/test/testother.cpp b/test/testother.cpp index c5d67c409..845b12fa2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5864,6 +5864,14 @@ private: " i = 1;\n" "}", 0, false, false, false, false); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (performance) Variable 'i' is reassigned a value before the old one has been used.\n", errout.str()); + + check("int foo() {\n" // #4420 + " int x;\n" + " bar(++x);\n" + " x = 5;\n" + " return bar(x);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void redundantMemWrite() {