Fixed #2488 (false positive with updating iterator in a for loop)

This commit is contained in:
Daniel Marjamäki 2011-01-20 20:48:35 +01:00
parent 10d2909c7c
commit 87e3e9e703
2 changed files with 21 additions and 2 deletions

View File

@ -284,9 +284,13 @@ public:
while (indentlevel > 0 && 0 != (tok = tok->next()))
{
if (tok->str() == "(")
++indentlevel;
tok = tok->link();
else if (tok->str() == ")")
--indentlevel;
break;
// reassigning iterator in loop head
else if (Token::Match(tok, "%var% =") && tok->str() == it->str())
break;
}
if (! Token::simpleMatch(tok, ") {"))

View File

@ -53,6 +53,7 @@ private:
TEST_CASE(erase3);
TEST_CASE(erase4);
TEST_CASE(erase5);
TEST_CASE(erase6);
TEST_CASE(eraseBreak);
TEST_CASE(eraseContinue);
TEST_CASE(eraseReturn1);
@ -502,6 +503,20 @@ private:
ASSERT_EQUALS("[test.cpp:8]: (error) Dangerous iterator usage after erase()-method.\n", errout.str());
}
void erase6()
{
check("void f() {\n"
" std::vector<int> vec(3);\n"
" std::vector<int>::iterator it;\n"
" std::vector<int>::iterator itEnd = vec.end();\n"
" for (it = vec.begin(); it != itEnd; it = vec.begin(), itEnd = vec.end())\n"
" {\n"
" vec.erase(it);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void eraseBreak()
{
check("void f()\n"