diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index d35624757..1b2b23183 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -60,6 +60,13 @@ void CheckStl::iterators() if (iteratorId == 0 || containerId == 0) continue; + // check that it's an iterator.. + { + const Variable *var = _tokenizer->getSymbolDatabase()->getVariableFromVarId(iteratorId); + if (!var || !Token::Match(var->nameToken()->previous(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) + continue; + } + // the validIterator flag says if the iterator has a valid value or not bool validIterator = true; const Token* validatingToken = 0; diff --git a/test/teststl.cpp b/test/teststl.cpp index 68a9519af..27f9c0d27 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -361,6 +361,13 @@ private: " if (it == map2.end()) { }" "}\n"); ASSERT_EQUALS("[test.cpp:5]: (error) Same iterator is used with both map1 and map2\n", errout.str()); + + check("void f(std::string &s) {\n" + " int pos = s.find(x);\n" + " s.erase(pos);\n" + " s.erase(pos);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // Dereferencing invalid pointer