Fixed #3001 (False Positive: Redundant assignment to itself)

This commit is contained in:
Daniel Marjamäki 2011-08-19 07:23:11 +02:00
parent e5ff920ea9
commit 4606251ce8
2 changed files with 25 additions and 1 deletions

View File

@ -577,7 +577,25 @@ void CheckOther::checkSelfAssignment()
tok->varId() && tok->varId() == tok->tokAt(2)->varId() &&
pod.find(tok->varId()) != pod.end())
{
selfAssignmentError(tok, tok->str());
bool err = true;
// no false positive for 'x = x ? x : 1;'
// if is simplified to 'if (x) { x = x ; } else { x = 1 ; }'. The simplification
// writes all tokens on 1 line so check if the lineno is the same for all the tokens.
if (Token::Match(tok->tokAt(-2), ") { %var% = %var% ; } else { %varid% =", tok->varId()))
{
// Find the 'if' token
const Token *tokif = tok->tokAt(-2)->link()->previous();
// find the '}' that terminates the 'else'-block
const Token *else_end = tok->tokAt(6)->link();
if (tokif && else_end && tokif->linenr() == else_end->linenr())
err = false;
}
if (err)
selfAssignmentError(tok, tok->str());
}
tok = Token::findmatch(tok->next(), selfAssignmentPattern);

View File

@ -1697,6 +1697,12 @@ private:
" func(x);\n"
"}");
ASSERT_EQUALS("", errout.str());
// ticket #3001 - false positive
check("void foo(int x) {\n"
" x = x ? x : 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void testScanf1()