Fixed #1834 (False positive: invalid iterator when erase() is used in if-else)

This commit is contained in:
Daniel Marjamäki 2010-07-09 10:50:24 +02:00
parent a60047227d
commit 2531bb73d7
2 changed files with 30 additions and 2 deletions

View File

@ -123,6 +123,10 @@ void CheckStl::iterators()
{
eraseByValueError(tok2, tok2->strAt(0), tok2->strAt(5));
}
else if (Token::Match(tok2, "return|break ;"))
{
validIterator = true;
}
}
}
}

View File

@ -53,7 +53,8 @@ private:
TEST_CASE(erase3);
TEST_CASE(erase4);
TEST_CASE(eraseBreak);
TEST_CASE(eraseReturn);
TEST_CASE(eraseReturn1);
TEST_CASE(eraseReturn2);
TEST_CASE(eraseGoto);
TEST_CASE(eraseAssign);
TEST_CASE(eraseErase);
@ -442,10 +443,12 @@ private:
ASSERT_EQUALS("", errout.str());
}
void eraseReturn()
void eraseReturn1()
{
check("void f()\n"
"{\n"
" std::vector<int> foo;\n"
" std::vector<int>::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" foo.erase(it);\n"
@ -455,6 +458,27 @@ private:
ASSERT_EQUALS("", errout.str());
}
void eraseReturn2()
{
check("void f()\n"
"{\n"
" std::vector<int> foo;\n"
" std::vector<int>::iterator it;\n"
" for (it = foo.begin(); it != foo.end(); ++it)\n"
" {\n"
" if (*it == 1) {\n"
" foo.erase(it);\n"
" return;\n"
" }\n"
" else {\n"
" foo.erase(it);\n"
" return;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void eraseGoto()
{
check("void f()\n"