From ef4ce6f46b0035b6017dad0dd472051c8190e0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 10 Oct 2010 14:28:14 +0200 Subject: [PATCH] STL: fixed false positives in the new double-increment check when iterator shadows outer iterator --- lib/checkstl.cpp | 8 ++++---- test/teststl.cpp | 14 +++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index dac8ce790..5ee3535e4 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -865,7 +865,7 @@ void CheckStl::missingComparison() continue; } - const std::string &itName(tok2->str()); + const unsigned int &iteratorId(tok2->varId()); const Token *incrementToken = 0; unsigned int indentlevel = 0; // Parse loop.. @@ -879,11 +879,11 @@ void CheckStl::missingComparison() break; --indentlevel; } - else if (tok3->str() == itName && Token::simpleMatch(tok3->next(), "++")) + else if (tok3->varId() == iteratorId && Token::simpleMatch(tok3->next(), "++")) incrementToken = tok3; - else if (tok3->str() == "++" && Token::simpleMatch(tok3->next(), itName.c_str())) + else if (tok3->str() == "++" && tok3->next() && tok3->next()->varId() == iteratorId) incrementToken = tok3; - else if (tok3->str() == itName && Token::Match(tok3->next(), "!=|==")) + else if (tok3->varId() == iteratorId && Token::Match(tok3->next(), "!=|==")) incrementToken = 0; } if (incrementToken) diff --git a/test/teststl.cpp b/test/teststl.cpp index ee3a560a2..4d9c02238 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -96,7 +96,8 @@ private: // missing inner comparison when incrementing iterator inside loop TEST_CASE(missingInnerComparison1); - TEST_CASE(missingInnerComparison2); + TEST_CASE(missingInnerComparison2); // no FP when there is comparison + TEST_CASE(missingInnerComparison3); // no FP when there is iterator shadowing } void check(const std::string &code) @@ -1068,6 +1069,17 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); } + + void missingInnerComparison3() + { + check("void f(std::set &ints) {\n" + " for (std::set::iterator it = ints.begin(); it != ints.end(); ++it) {\n" + " for (std::set::iterator it = ints2.begin(); it != ints2.end(); ++it)\n" + " { }\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestStl)