From 0864a0700a1dbb32feca2cc330a77a103f7f148b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 18 Oct 2010 20:05:54 +0200 Subject: [PATCH] Fixed #2108 (False positive: the loop might unintentionally skip an element in the container.) --- lib/checkstl.cpp | 2 ++ test/teststl.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index b9beae19a..9825c7967 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -899,6 +899,8 @@ void CheckStl::missingComparison() incrementToken = tok3; else if (tok3->varId() == iteratorId && Token::Match(tok3->next(), "!=|==")) incrementToken = 0; + else if (tok3->str() == "break") + incrementToken = 0; } if (incrementToken) missingComparisonError(incrementToken, tok2->tokAt(16)); diff --git a/test/teststl.cpp b/test/teststl.cpp index 4e6982106..87d21aa86 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -98,6 +98,7 @@ private: TEST_CASE(missingInnerComparison1); TEST_CASE(missingInnerComparison2); // no FP when there is comparison TEST_CASE(missingInnerComparison3); // no FP when there is iterator shadowing + TEST_CASE(missingInnerComparison4); // no FP when "break;" is used // catch common problems when using the string::c_str() function TEST_CASE(cstr); @@ -1094,6 +1095,19 @@ private: ASSERT_EQUALS("", errout.str()); } + void missingInnerComparison4() + { + check("function f1(std::list &l1) {\n" + " for(std::list::iterator i = l1.begin(); i != l1.end(); i++) {\n" + " if (*i == 44) {\n" + " l1.insert(++i, 55);\n" + " break;\n" + " }\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void cstr() { check("void f() {\n"