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)