STL: refactoring and fixing
This commit is contained in:
parent
89d94895a4
commit
87800185c4
|
@ -381,21 +381,28 @@ void CheckStl::pushback()
|
|||
if (Token::Match(tok2, "%varid% =", iteratorid))
|
||||
{
|
||||
if (Token::Match(tok2->tokAt(2), "%var% . begin|end|rbegin|rend ( )"))
|
||||
{
|
||||
vectorname = tok2->strAt(2);
|
||||
tok2 = tok2->tokAt(6);
|
||||
}
|
||||
else
|
||||
vectorname = "";
|
||||
invalidIterator = "";
|
||||
}
|
||||
|
||||
// push_back on vector..
|
||||
if (vectorname.size() && Token::Match(tok2, (vectorname + " . push_front|push_back|insert").c_str()))
|
||||
if (vectorname.size() && Token::Match(tok2, (vectorname + " . push_front|push_back|insert (").c_str()))
|
||||
{
|
||||
if (!invalidIterator.empty() && Token::Match(tok2->tokAt(2), "insert ( %varid% ,", iteratorid))
|
||||
{
|
||||
invalidIteratorError(tok2, invalidIterator, tok2->strAt(4));
|
||||
break;
|
||||
}
|
||||
|
||||
invalidIterator = tok2->strAt(2);
|
||||
if (!iteratorDeclaredInsideLoop)
|
||||
{
|
||||
while (tok2 && tok2->str() != "(")
|
||||
tok2 = tok2->next();
|
||||
tok2 = tok2 ? tok2->link() : 0;
|
||||
tok2 = tok2->tokAt(3)->link();
|
||||
if (!tok2)
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -495,10 +495,20 @@ private:
|
|||
|
||||
check("void f()\n"
|
||||
"{\n"
|
||||
" std::vector<int> ints;\n"
|
||||
" std::vector<int>::iterator iter = ints.begin();\n"
|
||||
" ints.insert(iter, 1);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("void f()\n"
|
||||
"{\n"
|
||||
" std::vector<int> ints;\n"
|
||||
" std::vector<int>::iterator iter = ints.begin();\n"
|
||||
" ints.insert(iter, 1);\n"
|
||||
" ints.insert(iter, 2);\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:6]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue