diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index d25dc614c..5e0372211 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -67,7 +67,20 @@ void CheckStl::iterators() else if (Token::Match(tok2, "%var% . insert|erase ( %varid% )|,", iteratorId)) { if (tok2->varId() != containerId && tok2->tokAt(5)->str() != ".") + { + // skip error message if container is a set.. + if (tok2->varId() > 0) + { + const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", tok2->varId()); + while (decltok && !Token::Match(decltok, "[;{},(]")) + decltok = decltok->previous(); + if (Token::Match(decltok, "%any% const| std :: set")) + continue; // No warning + } + + // Show error message, mismatching iterator is used. iteratorsError(tok2, tok->strAt(2), tok2->str()); + } else if (tok2->strAt(2) == std::string("erase")) validIterator = false; diff --git a/test/teststl.cpp b/test/teststl.cpp index 40484306e..906946d8c 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -39,6 +39,7 @@ private: TEST_CASE(iterator3); TEST_CASE(iterator4); TEST_CASE(iterator5); + TEST_CASE(iterator6); TEST_CASE(dereference); TEST_CASE(dereference_member); @@ -163,6 +164,19 @@ private: ASSERT_EQUALS("[test.cpp:5]: (error) mismatching containers\n", errout.str()); } + void iterator6() + { + // Ticket #1357 + check("void foo(const std::set &ints1)\n" + "{\n" + " std::set ints2;\n" + " std::set::iterator it1 = ints1.begin();\n" + " std::set::iterator it2 = ints1.end();\n" + " ints2.insert(it1, it2);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + // Dereferencing invalid pointer void dereference() {