STL: It is ok to compare vector iterators with <
https://apps.sourceforge.net/trac/cppcheck/ticket/313
This commit is contained in:
parent
2bd80a7335
commit
2e61201c18
|
@ -335,24 +335,40 @@ void CheckStl::stlBoundries()
|
||||||
{
|
{
|
||||||
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
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 ( ) ") &&
|
const unsigned int iteratorid(tok->tokAt(3)->varId());
|
||||||
tok2->str() == tok2->tokAt(8)->str() &&
|
if (iteratorid == 0)
|
||||||
tok2->tokAt(2)->str() == tok2->tokAt(10)->str())
|
continue;
|
||||||
|
|
||||||
|
// Using "iterator < ..." is not allowed
|
||||||
|
unsigned int indentlevel = 0;
|
||||||
|
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
|
||||||
{
|
{
|
||||||
stlBoundriesError(tok2);
|
if (tok2->str() == "{")
|
||||||
break;
|
++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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -377,10 +377,19 @@ private:
|
||||||
{
|
{
|
||||||
check("void f()\n"
|
check("void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
" std::list<int>::iterator it;\n"
|
||||||
" for (it = ab.begin(); it < ab.end(); ++it)\n"
|
" for (it = ab.begin(); it < ab.end(); ++it)\n"
|
||||||
" ;\n"
|
" ;\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());
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue