STL: fixed false positives for the new double-increment check

This commit is contained in:
Daniel Marjamäki 2010-10-10 11:22:44 +02:00
parent 835b511bee
commit ae0528ef59
2 changed files with 16 additions and 1 deletions

View File

@ -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)

View File

@ -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<int> &ints) {\n"
" for (std::set<int>::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)