From 87e3e9e703a51acba2cf0791a0295798ab0eee97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 20 Jan 2011 20:48:35 +0100 Subject: [PATCH] Fixed #2488 (false positive with updating iterator in a for loop) --- lib/checkstl.cpp | 8 ++++++-- test/teststl.cpp | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index bb5893f48..fc90339bb 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -284,9 +284,13 @@ public: while (indentlevel > 0 && 0 != (tok = tok->next())) { if (tok->str() == "(") - ++indentlevel; + tok = tok->link(); else if (tok->str() == ")") - --indentlevel; + break; + + // reassigning iterator in loop head + else if (Token::Match(tok, "%var% =") && tok->str() == it->str()) + break; } if (! Token::simpleMatch(tok, ") {")) diff --git a/test/teststl.cpp b/test/teststl.cpp index 2d27a09e4..eabb2c2c3 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -53,6 +53,7 @@ private: TEST_CASE(erase3); TEST_CASE(erase4); TEST_CASE(erase5); + TEST_CASE(erase6); TEST_CASE(eraseBreak); TEST_CASE(eraseContinue); TEST_CASE(eraseReturn1); @@ -502,6 +503,20 @@ private: ASSERT_EQUALS("[test.cpp:8]: (error) Dangerous iterator usage after erase()-method.\n", errout.str()); } + void erase6() + { + check("void f() {\n" + " std::vector vec(3);\n" + " std::vector::iterator it;\n" + " std::vector::iterator itEnd = vec.end();\n" + " for (it = vec.begin(); it != itEnd; it = vec.begin(), itEnd = vec.end())\n" + " {\n" + " vec.erase(it);\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void eraseBreak() { check("void f()\n"