Fixed #2481 (false positive with break: After insert, the iterator '*' may be invalid)
This commit is contained in:
parent
9dce0dd75c
commit
a21f8eec7c
|
@ -594,7 +594,7 @@ void CheckStl::pushback()
|
|||
break;
|
||||
--indent3;
|
||||
}
|
||||
else if (tok3->str() == "break")
|
||||
else if (tok3->str() == "break" || tok3->str() == "return")
|
||||
{
|
||||
pushbackTok = 0;
|
||||
break;
|
||||
|
@ -634,12 +634,7 @@ void CheckStl::pushback()
|
|||
}
|
||||
|
||||
invalidIterator = tok2->strAt(2);
|
||||
if (!iteratorDeclaredInsideLoop)
|
||||
{
|
||||
tok2 = tok2->tokAt(3)->link();
|
||||
if (!tok2)
|
||||
break;
|
||||
}
|
||||
tok2 = tok2->tokAt(3)->link();
|
||||
}
|
||||
|
||||
else if (tok2->str() == "return" || tok2->str() == "break")
|
||||
|
|
|
@ -76,6 +76,7 @@ private:
|
|||
TEST_CASE(pushback10);
|
||||
|
||||
TEST_CASE(insert1);
|
||||
TEST_CASE(insert2);
|
||||
|
||||
TEST_CASE(invalidcode);
|
||||
|
||||
|
@ -904,7 +905,29 @@ private:
|
|||
ASSERT_EQUALS("[test.cpp:6]: (error) After insert, the iterator 'iter' may be invalid\n", errout.str());
|
||||
}
|
||||
|
||||
void insert2()
|
||||
{
|
||||
// Ticket: #2169
|
||||
check("void f(std::vector<int> &vec) {\n"
|
||||
" for(std::vector<int>::iterator iter = vec.begin(); iter != vec.end(); ++iter)\n"
|
||||
" {\n"
|
||||
" vec.insert(iter, 0);\n"
|
||||
" break;\n"
|
||||
" }\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("void f(std::vector<int> &vec) {\n"
|
||||
" for(std::vector<int>::iterator iter = vec.begin(); iter != vec.end(); ++iter)\n"
|
||||
" {\n"
|
||||
" if (*it == 0) {\n"
|
||||
" vec.insert(iter, 0);\n"
|
||||
" return;\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void invalidcode()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue