From 2566fd09da21dd52fa003f0c00645ca858a8fe22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 4 Aug 2016 09:35:16 +0200 Subject: [PATCH] Fixed #5803 (False positive: Same iterator is used with different containers - insert() from range of different container) --- lib/checkstl.cpp | 14 ++++++++++++++ test/teststl.cpp | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index f8130d9e7..2fca0f994 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -165,6 +165,20 @@ void CheckStl::iterators() if (itTok->previous()->str() == "*") continue; + // inserting iterator range.. + if (tok2->strAt(2) == "insert") { + const Token *par2 = itTok->nextArgument(); + while (par2 && par2->str() != ")") { + if (par2->varId() == container->declarationId()) + break; + if (par2->str() == "(") + par2 = par2->link(); + par2 = par2->next(); + } + if (par2->varId() == container->declarationId()) + continue; + } + // Show error message, mismatching iterator is used. iteratorsError(tok2, container->name(), tok2->str()); } diff --git a/test/teststl.cpp b/test/teststl.cpp index 1e0a5b09e..e1a12db46 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -201,6 +201,14 @@ private: " l2.insert(it, 0);\n" "}"); ASSERT_EQUALS("[test.cpp:6]: (error) Same iterator is used with different containers 'l1' and 'l2'.\n", errout.str()); + + check("void foo() {\n" // #5803 + " list l1;\n" + " list l2;\n" + " list::iterator it = l1.begin();\n" + " l2.insert(it, l1.end());\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void iterator4() {