Fixed #3201 (Checking iterators from different objects)

This commit is contained in:
Daniel Marjamäki 2011-12-26 10:30:39 +01:00
parent fd4bc12ed3
commit 2d05cae13b
2 changed files with 15 additions and 2 deletions

View File

@ -50,7 +50,8 @@ void CheckStl::iterators()
// for (it = foo.begin(); it != bar.end(); ++it)
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
// Locate an iterator..
if (!Token::Match(tok, "%var% = %var% . begin|rbegin|cbegin|crbegin ( ) ;|+"))
if (!Token::Match(tok, "%var% = %var% . begin|rbegin|cbegin|crbegin ( ) ;|+") &&
!(Token::Match(tok, "%var% = %var% . find (") && Token::simpleMatch(tok->linkAt(5), ") ;")))
continue;
// Get variable ids for both the iterator and container
@ -81,7 +82,7 @@ void CheckStl::iterators()
break;
// Is iterator compared against different container?
if (Token::Match(tok2, "%varid% != %var% . end|rend|cend|crend ( )", iteratorId) && tok2->tokAt(2)->varId() != containerId) {
if (Token::Match(tok2, "%varid% !=|== %var% . end|rend|cend|crend ( )", iteratorId) && tok2->tokAt(2)->varId() != containerId) {
iteratorsError(tok2, tok->strAt(2), tok2->strAt(2));
tok2 = tok2->tokAt(6);
}

View File

@ -43,6 +43,7 @@ private:
TEST_CASE(iterator9);
TEST_CASE(iterator10);
TEST_CASE(iterator11);
TEST_CASE(iterator12);
TEST_CASE(dereference);
TEST_CASE(dereference_member);
@ -351,6 +352,17 @@ private:
ASSERT_EQUALS("", errout.str());
}
void iterator12() {
// Ticket #3201
check("void f() {\n"
" std::map<int, int> map1;\n"
" std::map<int, int> map2;\n"
" std::map<int, int>::const_iterator it = map1.find(123);\n"
" if (it == map2.end()) { }"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with both map1 and map2\n", errout.str());
}
// Dereferencing invalid pointer
void dereference() {
check("void f()\n"