diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 8f7bf09ff..c6a113c6e 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -30,6 +30,11 @@ CheckStl instance; // Error message for bad iterator usage.. +void CheckStl::invalidIteratorError(const Token *tok, const std::string &iteratorName) +{ + reportError(tok, Severity::error, "invalidIterator", "Invalid iterator: " + iteratorName); +} + void CheckStl::iteratorsError(const Token *tok, const std::string &container1, const std::string &container2) { reportError(tok, Severity::error, "iterators", "Same iterator is used with both " + container1 + " and " + container2); @@ -66,6 +71,9 @@ void CheckStl::iterators() } else if (Token::Match(tok2, "%var% . insert|erase ( %varid% )|,", iteratorId)) { + if (!validIterator) + invalidIteratorError(tok2, tok2->strAt(4)); + if (tok2->varId() != containerId && tok2->tokAt(5)->str() != ".") { // skip error message if container is a set.. diff --git a/lib/checkstl.h b/lib/checkstl.h index 1f79561c3..5ffe907d4 100644 --- a/lib/checkstl.h +++ b/lib/checkstl.h @@ -117,6 +117,7 @@ private: void eraseCheckLoop(const Token *it); void stlOutOfBoundsError(const Token *tok, const std::string &num, const std::string &var); + void invalidIteratorError(const Token *tok, const std::string &iteratorName); void iteratorsError(const Token *tok, const std::string &container1, const std::string &container2); void mismatchingContainersError(const Token *tok); void eraseError(const Token *tok); @@ -128,6 +129,7 @@ private: void getErrorMessages() { + invalidIteratorError(0, "iterator"); iteratorsError(0, "container1", "container2"); mismatchingContainersError(0); dereferenceErasedError(0, "iter"); diff --git a/test/teststl.cpp b/test/teststl.cpp index 8753f9bec..a06b03874 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -437,8 +437,7 @@ private: " ints.erase(iter);\n" " ints.erase(iter);\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Erasing invalid iterator\n", errout.str()); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:6]: (error) Invalid iterator: iter\n", errout.str()); }