diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 582bdcc93..d35624757 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -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); } diff --git a/test/teststl.cpp b/test/teststl.cpp index c5d552a4d..68a9519af 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -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 map1;\n" + " std::map map2;\n" + " std::map::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"