From ae0528ef59e1a9b10f28d990cc4bfc1e53ea3894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 10 Oct 2010 11:22:44 +0200 Subject: [PATCH] STL: fixed false positives for the new double-increment check --- lib/checkstl.cpp | 2 +- test/teststl.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 25cac8b15..dac8ce790 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -883,7 +883,7 @@ void CheckStl::missingComparison() incrementToken = tok3; else if (tok3->str() == "++" && Token::simpleMatch(tok3->next(), itName.c_str())) incrementToken = tok3; - else if (tok3->str() == itName && Token::simpleMatch(tok3->next(), "!=")) + else if (tok3->str() == itName && Token::Match(tok3->next(), "!=|==")) incrementToken = 0; } if (incrementToken) diff --git a/test/teststl.cpp b/test/teststl.cpp index b5ea268d4..ee3a560a2 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -96,6 +96,7 @@ private: // missing inner comparison when incrementing iterator inside loop TEST_CASE(missingInnerComparison1); + TEST_CASE(missingInnerComparison2); } void check(const std::string &code) @@ -1053,6 +1054,20 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) The iterator is incremented at line 4 and then at line 2. The loop might unintentionally skip an element in the container. There is no comparison between these increments to prevent that the iterator is incremented beyond the end.\n", errout.str()); } + + void missingInnerComparison2() + { + check("void f(std::set &ints) {\n" + " for (std::set::iterator it = ints.begin(); it != ints.end(); ++it) {\n" + " if (a) {\n" + " it++;\n" + " if (it == ints.end())\n" + " return;\n" + " }\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestStl)