diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 914753f6a..ad1539a50 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -796,9 +796,15 @@ void CheckStl::mismatchingContainerIterator() continue; if (val.lifetimeKind != ValueFlow::Value::LifetimeKind::Iterator) continue; - if (isSameExpression(true, false, tok, val.tokvalue, mSettings->library, false, false)) - continue; - mismatchingContainerIteratorError(tok, iterTok); + for (const LifetimeToken& lt:getLifetimeTokens(tok)) { + if (lt.inconclusive) + continue; + const Token* contTok = lt.token; + if (isSameExpression(true, false, contTok, val.tokvalue, mSettings->library, false, false)) + continue; + mismatchingContainerIteratorError(tok, iterTok); + } + } } } diff --git a/test/teststl.cpp b/test/teststl.cpp index 24f59c045..439e1a413 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -1351,6 +1351,20 @@ private: " return a;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2]: (error) Iterator 'b.begin()' from different container 'a' are used together.\n", errout.str()); + + // #9973 + check("void f() {\n" + " std::list l1;\n" + " std::list l2;\n" + " std::list& l = l2;\n" + " for (auto it = l.begin(); it != l.end(); ++it) {\n" + " if (*it == 1) {\n" + " l.erase(it);\n" + " break;\n" + " }\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } // Dereferencing invalid pointer