Fixed #2317 ((style) Variable is allocated memory that is never used)

This commit is contained in:
Daniel Marjamäki 2010-12-20 18:31:16 +01:00
parent cdc8801be0
commit f73cce9eca
2 changed files with 15 additions and 4 deletions

View File

@ -1641,7 +1641,10 @@ void CheckOther::functionVariableUsage()
else if (var && var->_type == Variables::pointer && else if (var && var->_type == Variables::pointer &&
Token::Match(start, "%var% = new|malloc|calloc|g_malloc|kmalloc|vmalloc")) Token::Match(start, "%var% = new|malloc|calloc|g_malloc|kmalloc|vmalloc"))
{ {
variables.allocateMemory(varid1); if (start->strAt(2) == "new" && !start->tokAt(3)->isStandardType())
variables.write(varid1);
else
variables.allocateMemory(varid1);
} }
else else
{ {

View File

@ -2550,11 +2550,19 @@ private:
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
" string* txt = new string(\"test\");\n" " Fred* fred = new Fred;\n"
" std::cout << \"test\" << std::endl;\n" " std::cout << \"test\" << std::endl;\n"
" delete txt;\n" " delete fred;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'txt' is allocated memory that is never used\n", errout.str()); ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" Fred* fred = malloc(sizeof(Fred));\n"
" std::cout << \"test\" << std::endl;\n"
" free(fred);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'fred' is allocated memory that is never used\n", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"