stl: added testcase for bad iterator usage

This commit is contained in:
Daniel Marjamäki 2009-02-10 20:01:39 +00:00
parent 4660b7648d
commit 71b4e5a912
2 changed files with 28 additions and 0 deletions

View File

@ -35,6 +35,7 @@ void CheckStl::iterators()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
// it = foo.begin(); it != bar.end()
if (Token::Match(tok, "%var% = %var% . begin ( ) ; %var% != %var% . end ( ) ;"))
{
// Different iterators..
@ -45,6 +46,19 @@ void CheckStl::iterators()
continue;
_errorLogger->iteratorUsage(_tokenizer, tok, tok->tokAt(2)->str(), tok->tokAt(10)->str());
}
// it = foo.begin();
// while (it != bar.end())
if (Token::Match(tok, "%var% = %var% . begin ( ) ; while ( %var% != %var% . end ( )"))
{
// Different iterators..
if (tok->str() != tok->tokAt(10)->str())
continue;
// Same container..
if (tok->tokAt(2)->str() == tok->tokAt(12)->str())
continue;
_errorLogger->iteratorUsage(_tokenizer, tok, tok->tokAt(2)->str(), tok->tokAt(12)->str());
}
}
}

View File

@ -37,6 +37,7 @@ private:
void run()
{
TEST_CASE(iterator1);
TEST_CASE(iterator2);
TEST_CASE(STLSize);
TEST_CASE(STLSizeNoErr);
}
@ -68,6 +69,19 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
}
void iterator2()
{
check("void foo()\n"
"{\n"
" it = foo.begin();\n"
" while (it != bar.end())\n"
" {\n"
" ++it;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Same iterator is used with both foo and bar\n", errout.str());
}
void STLSize()
{