Fixed handling of std::vector::insert() in CheckStl::pushback()
This commit is contained in:
parent
c24527dbde
commit
34b1d75a10
|
@ -638,7 +638,7 @@ void CheckStl::pushback()
|
|||
if (tok3->str() == "break" || tok3->str() == "return") {
|
||||
pushbackTok = 0;
|
||||
break;
|
||||
} else if (Token::Match(tok3, "%varid% . push_front|push_back|insert|reserve|resize|clear (", varId)) {
|
||||
} else if (Token::Match(tok3, "%varid% . push_front|push_back|insert|reserve|resize|clear (", varId) && !tok3->previous()->isAssignmentOp()) {
|
||||
pushbackTok = tok3->tokAt(2);
|
||||
}
|
||||
}
|
||||
|
@ -649,9 +649,13 @@ void CheckStl::pushback()
|
|||
|
||||
// Assigning iterator..
|
||||
if (Token::Match(tok2, "%varid% =", iteratorid)) {
|
||||
if (Token::Match(tok2->tokAt(2), "%var% . begin|end|rbegin|rend|cbegin|cend|crbegin|crend ( )")) {
|
||||
if (Token::Match(tok2->tokAt(2), "%var% . begin|end|rbegin|rend|cbegin|cend|crbegin|crend|insert (")) {
|
||||
if (!invalidIterator.empty() && Token::Match(tok2->tokAt(4), "insert ( %varid% ,", iteratorid)) {
|
||||
invalidIteratorError(tok2, invalidIterator, tok2->strAt(6));
|
||||
break;
|
||||
}
|
||||
vectorid = tok2->tokAt(2)->varId();
|
||||
tok2 = tok2->tokAt(6);
|
||||
tok2 = tok2->linkAt(5);
|
||||
} else {
|
||||
vectorid = 0;
|
||||
}
|
||||
|
|
|
@ -1161,6 +1161,31 @@ private:
|
|||
" return i->foo;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) After insert(), the iterator 'i' may be invalid.\n", errout.str());
|
||||
|
||||
check("void f(const std::vector<Bar>& bars) {\n"
|
||||
" for(std::vector<Bar>::iterator i = bars.begin(); i != bars.end(); ++i) {\n"
|
||||
" i = bars.insert(i, bar);\n"
|
||||
" }\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
check("void f(const std::vector<Bar>& bars) {\n"
|
||||
" for(std::vector<Bar>::iterator i = bars.begin(); i != bars.end(); ++i) {\n"
|
||||
" bars.insert(i, bar);\n"
|
||||
" i = bars.insert(i, bar);\n"
|
||||
" }\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) After insert(), the iterator 'i' may be invalid.\n"
|
||||
"[test.cpp:4]: (error) After insert(), the iterator 'i' may be invalid.\n", errout.str());
|
||||
|
||||
check("void* f(const std::vector<Bar>& bars) {\n"
|
||||
" std::vector<Bar>::iterator i = bars.begin();\n"
|
||||
" bars.insert(i, Bar());\n"
|
||||
" i = bars.insert(i, Bar());\n"
|
||||
" void* v = &i->foo;\n"
|
||||
" return v;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) After insert(), the iterator 'i' may be invalid.\n", errout.str());
|
||||
}
|
||||
|
||||
void insert2() {
|
||||
|
|
Loading…
Reference in New Issue