Fixed #4420: Prefix increment is only suspicious, if its return value is not used.

This commit is contained in:
PKEuS 2013-06-10 13:02:02 -07:00
parent 28c0045f36
commit 4efccc2c5d
2 changed files with 9 additions and 1 deletions

View File

@ -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

View File

@ -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() {