From 252c3a17fa9adde28a4bd6f9dafb3afc3fba22a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Dec 2019 12:13:15 +0100 Subject: [PATCH] Fixed #7107 (False positive iterators - using alias for container) --- lib/checkstl.cpp | 15 +++++++++++++++ test/teststl.cpp | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 3af61dacc..c789d9b79 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -493,6 +493,21 @@ void CheckStl::iterators() continue; } + // Not different containers if a reference is used.. + if (containerToken && containerToken->variable() && containerToken->variable()->isReference()) { + const Token *nameToken = containerToken->variable()->nameToken(); + if (Token::Match(nameToken, "%name% =")) { + const Token *name1 = nameToken->tokAt(2); + const Token *name2 = tok2; + while (Token::Match(name1, "%name%|.|::") && name2 && name1->str() == name2->str()) { + name1 = name1->next(); + name2 = name2->next(); + } + if (!Token::simpleMatch(name1, ";") || !Token::Match(name2, "[;,()=]")) + continue; + } + } + // Show error message, mismatching iterator is used. iteratorsError(tok2, getContainerName(containerToken), getContainerName(tok2)); } diff --git a/test/teststl.cpp b/test/teststl.cpp index 69acecd14..389709bc7 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -64,6 +64,7 @@ private: TEST_CASE(iterator19); TEST_CASE(iterator20); TEST_CASE(iterator21); + TEST_CASE(iterator22); TEST_CASE(iteratorExpression); TEST_CASE(iteratorSameExpression); @@ -1017,7 +1018,15 @@ private: " }\n" "}"); ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6] -> [test.cpp:5]: (error) Comparison of iterators from containers 'l1' and 'l2'.\n", errout.str()); + } + void iterator22() { // #7107 + check("void foo() {\n" + " std::list &l = x.l;\n" + " std::list::iterator it = l.find(123);\n" + " x.l.erase(it);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void iteratorExpression() {