add new (nothrow) support to CheckUnusedVar::checkFunctionVariableUsage

This commit is contained in:
Robert Reif 2011-08-31 06:39:39 -04:00
parent acebc635b5
commit 477d1e92c9
2 changed files with 38 additions and 1 deletions

View File

@ -1204,8 +1204,15 @@ void CheckUnusedVar::checkFunctionVariableUsage()
if (start->strAt(2) == "new") if (start->strAt(2) == "new")
{ {
const Token *type = start->tokAt(3);
// skip nothrow
if (Token::Match(type, "( nothrow )") ||
Token::Match(type, "( std :: nothrow )"))
type = type->link()->next();
// is it a user defined type? // is it a user defined type?
if (!start->tokAt(3)->isStandardType()) if (!type->isStandardType())
{ {
if (!isRecordTypeWithoutSideEffects(start)) if (!isRecordTypeWithoutSideEffects(start))
allocate = false; allocate = false;

View File

@ -2711,6 +2711,20 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'ptr' is allocated memory that is never used\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'ptr' is allocated memory that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" char* ptr = new ( nothrow ) char[16];\n"
" delete[] ptr;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'ptr' is allocated memory that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" char* ptr = new ( std::nothrow ) char[16];\n"
" delete[] ptr;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'ptr' is allocated memory that is never used\n", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
" char* ptr = new char;\n" " char* ptr = new char;\n"
@ -2821,6 +2835,22 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'ptr' is allocated memory that is never used\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'ptr' is allocated memory that is never used\n", errout.str());
functionVariableUsage("struct Fred { int i; };\n"
"void foo()\n"
"{\n"
" Fred* ptr = new (nothrow ) Fred();\n"
" delete ptr;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'ptr' is allocated memory that is never used\n", errout.str());
functionVariableUsage("struct Fred { int i; };\n"
"void foo()\n"
"{\n"
" Fred* ptr = new (std::nothrow) Fred();\n"
" delete ptr;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'ptr' is allocated memory that is never used\n", errout.str());
functionVariableUsage("struct Fred { int i; };\n" functionVariableUsage("struct Fred { int i; };\n"
"void foo()\n" "void foo()\n"
"{\n" "{\n"