Fixed #2108 (False positive: the loop might unintentionally skip an element in the container.)

This commit is contained in:
Daniel Marjamäki 2010-10-18 20:05:54 +02:00
parent e54129fa8d
commit 0864a0700a
2 changed files with 16 additions and 0 deletions

View File

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

View File

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