STL: It is ok to compare vector iterators with <

https://apps.sourceforge.net/trac/cppcheck/ticket/313
This commit is contained in:
Daniel Marjamäki 2009-05-17 18:58:32 +02:00
parent 2bd80a7335
commit 2e61201c18
2 changed files with 38 additions and 13 deletions

View File

@ -335,24 +335,40 @@ void CheckStl::stlBoundries()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "for ("))
// Declaring iterator..
if (Token::Match(tok, "list <"))
{
for (const Token *tok2 = tok->tokAt(2); tok2 && tok2->str() != ";"; tok2 = tok2->next())
while (tok && tok->str() != ">")
tok = tok->next();
if (!tok)
break;
if (Token::Match(tok, "> :: iterator|const_iterator %var% =|;"))
{
if (Token::Match(tok2, "%var% = %var% . begin ( ) ; %var% < %var% . end ( ) ") &&
tok2->str() == tok2->tokAt(8)->str() &&
tok2->tokAt(2)->str() == tok2->tokAt(10)->str())
const unsigned int iteratorid(tok->tokAt(3)->varId());
if (iteratorid == 0)
continue;
// Using "iterator < ..." is not allowed
unsigned int indentlevel = 0;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
stlBoundriesError(tok2);
break;
if (tok2->str() == "{")
++indentlevel;
else if (tok2->str() == "}")
{
if (indentlevel == 0)
break;
--indentlevel;
}
else if (tok2->varId() == iteratorid && tok2->next() && tok2->next()->str() == "<")
{
stlBoundriesError(tok2);
break;
}
}
}
}
if (Token::Match(tok, "while ( %var% < %var% . end ( )"))
{
stlBoundriesError(tok);
}
}
}

View File

@ -377,10 +377,19 @@ private:
{
check("void f()\n"
"{\n"
" std::list<int>::iterator it;\n"
" for (it = ab.begin(); it < ab.end(); ++it)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) STL range check should be using != and not < since the order of the pointers isn't guaranteed\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) STL range check should be using != and not < since the order of the pointers isn't guaranteed\n", errout.str());
check("void f()\n"
"{\n"
" std::vector<int>::iterator it;\n"
" for (it = ab.begin(); it < ab.end(); ++it)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};