From 477d1e92c9e8dfaf2fb5479f6cb6248e4fe43d92 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 31 Aug 2011 06:39:39 -0400 Subject: [PATCH] add new (nothrow) support to CheckUnusedVar::checkFunctionVariableUsage --- lib/checkunusedvar.cpp | 9 ++++++++- test/testunusedvar.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index ac9d472eb..2e5c3e430 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1204,8 +1204,15 @@ void CheckUnusedVar::checkFunctionVariableUsage() 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? - if (!start->tokAt(3)->isStandardType()) + if (!type->isStandardType()) { if (!isRecordTypeWithoutSideEffects(start)) allocate = false; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index d1e03bdbc..d0811621b 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -2711,6 +2711,20 @@ private: "}\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 ( 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" "{\n" " char* ptr = new char;\n" @@ -2821,6 +2835,22 @@ private: "}\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 (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" "void foo()\n" "{\n"