Fixed #2502 (False positive: redundant assingment of object to itself)

This commit is contained in:
Daniel Marjamäki 2011-02-12 14:27:07 +01:00
parent ee0f5ff7b3
commit 2657d36d03
2 changed files with 17 additions and 3 deletions

View File

@ -317,12 +317,21 @@ void CheckOther::checkSelfAssignment()
if (!_settings->_checkCodingStyle)
return;
// POD variables..
std::set<unsigned int> pod;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (tok->isStandardType() && tok->next()->varId() && Token::Match(tok->tokAt(2), "[,);]"))
pod.insert(tok->next()->varId());
}
const char selfAssignmentPattern[] = "%var% = %var% ;|=|)";
const Token *tok = Token::findmatch(_tokenizer->tokens(), selfAssignmentPattern);
while (tok)
{
if (Token::Match(tok->previous(), "[;{}]") &&
tok->varId() && tok->varId() == tok->tokAt(2)->varId())
tok->varId() && tok->varId() == tok->tokAt(2)->varId() &&
pod.find(tok->varId()) != pod.end())
{
selfAssignmentError(tok, tok->str());
}

View File

@ -1157,14 +1157,12 @@ private:
check("void foo()\n"
"{\n"
" int x = x;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of \"x\" to itself\n", errout.str());
check("void foo()\n"
"{\n"
" std::string var = var = \"test\";\n"
" return 0;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant assignment of \"var\" to itself\n", "", errout.str());
@ -1182,6 +1180,13 @@ private:
" *x = x;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// non-primitive type -> there might be some side effects
check("void foo()\n"
"{\n"
" Fred fred; fred = fred;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void testScanf1()