diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index a172ee621..bdfb83ceb 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1370,7 +1370,7 @@ void CheckStl::eraseCheckLoopVar(const Scope &scope, const Variable *var) // Vector erases are handled by invalidContainer check if (isVector(tok->tokAt(-3))) continue; - if (Token::simpleMatch(tok->astParent(), "=")) + if (Token::Match(tok->astParent(), "=|return")) continue; // Iterator is invalid.. int indentlevel = 0U; diff --git a/test/teststl.cpp b/test/teststl.cpp index b43544012..ca5cec33f 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -4285,6 +4285,15 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:9]: (error) Out of bounds access in expression 'e[d].begin()' because 'e[d]' is empty.\n", errout.str()); + + // #10151 + check("std::set::iterator f(std::set& s) {\n" + "for (auto it = s.begin(); it != s.end(); ++it)\n" + " if (*it == 42)\n" + " return s.erase(it);\n" + " return s.end();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void dereferenceInvalidIterator2() {