Fixed #931 (Invalid interator false positive with identical variable names)

This commit is contained in:
Daniel Marjamäki 2009-11-10 19:07:04 +01:00
parent e0b1303b50
commit b470ea06b8
2 changed files with 30 additions and 7 deletions

View File

@ -330,7 +330,7 @@ void CheckStl::pushback()
tok = tok->tokAt(3);
}
std::string vectorname;
unsigned int vectorid = 0;
int indent = 0;
std::string invalidIterator;
for (const Token *tok2 = tok; indent >= 0 && tok2; tok2 = tok2->next())
@ -389,16 +389,18 @@ void CheckStl::pushback()
{
if (Token::Match(tok2->tokAt(2), "%var% . begin|end|rbegin|rend ( )"))
{
vectorname = tok2->strAt(2);
vectorid = tok2->tokAt(2)->varId();
tok2 = tok2->tokAt(6);
}
else
vectorname = "";
{
vectorid = 0;
}
invalidIterator = "";
}
// push_back on vector..
if (vectorname.size() && Token::Match(tok2, (vectorname + " . push_front|push_back|insert (").c_str()))
if (vectorid > 0 && Token::Match(tok2, "%varid% . push_front|push_back|insert (", vectorid))
{
if (!invalidIterator.empty() && Token::Match(tok2->tokAt(2), "insert ( %varid% ,", iteratorid))
{
@ -424,8 +426,6 @@ void CheckStl::pushback()
invalidIteratorError(tok2, invalidIterator, tok2->str());
}
}
}
}

View File

@ -60,6 +60,7 @@ private:
TEST_CASE(pushback6);
TEST_CASE(pushback7);
TEST_CASE(pushback8);
TEST_CASE(pushback9);
TEST_CASE(insert1);
@ -357,7 +358,7 @@ private:
void pushback1()
{
check("void f()\n"
check("void f(const std::vector<int> &foo)\n"
"{\n"
" std::vector<int>::const_iterator it = foo.begin();\n"
" foo.push_back(123);\n"
@ -488,6 +489,28 @@ private:
ASSERT_EQUALS("[test.cpp:8]: (error) After push_back, the iterator 'end' may be invalid\n", errout.str());
}
void pushback9()
{
check("struct A {\n"
" std::vector<int> ints;\n"
"};\n"
"\n"
"void f()\n"
"{\n"
" std::vector<int> ints;\n"
" A a;\n"
" std::vector<int>::const_iterator i = ints.begin();\n"
" std::vector<int>::const_iterator e = ints.end();\n"
" while (i != e)\n"
" {\n"
" a.ints.push_back(*i);\n"
" ++i;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void insert1()
{