Merge pull request #56 from richq/autoptr

Extra check for auto_ptr new[]
This commit is contained in:
Daniel Marjamäki 2011-11-05 10:19:07 -07:00
commit 20677a6d54
2 changed files with 10 additions and 0 deletions

View File

@ -1101,6 +1101,10 @@ void CheckStl::checkAutoPointer()
while (tok2) { while (tok2) {
if (Token::Match(tok2, "> %var%")) { if (Token::Match(tok2, "> %var%")) {
const Token *tok3 = tok2->next()->next(); const Token *tok3 = tok2->next()->next();
if (Token::Match(tok3, "( new %type% [")) {
autoPointerArrayError(tok2->next());
break;
}
while (tok3 && tok3->str() != ";") { while (tok3 && tok3->str() != ";") {
tok3 = tok3->next(); tok3 = tok3->next();
} }

View File

@ -1433,6 +1433,12 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Object pointed by an 'auto_ptr' is destroyed using operator 'delete'. You should not use 'auto_ptr' for pointers obtained with operator 'new[]'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Object pointed by an 'auto_ptr' is destroyed using operator 'delete'. You should not use 'auto_ptr' for pointers obtained with operator 'new[]'.\n", errout.str());
check("void f() \n"
"{\n"
" auto_ptr<T> p2( new T[5] );\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Object pointed by an 'auto_ptr' is destroyed using operator 'delete'. You should not use 'auto_ptr' for pointers obtained with operator 'new[]'.\n", errout.str());
check("void f() \n" check("void f() \n"
"{\n" "{\n"
" auto_ptr<T> p2;\n" " auto_ptr<T> p2;\n"