From f73cce9ecac55d67f448ff4dde5fd370ff12ce2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 20 Dec 2010 18:31:16 +0100 Subject: [PATCH] Fixed #2317 ((style) Variable is allocated memory that is never used) --- lib/checkother.cpp | 5 ++++- test/testunusedvar.cpp | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4dfc80b5a..89b27b0cb 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1641,7 +1641,10 @@ void CheckOther::functionVariableUsage() else if (var && var->_type == Variables::pointer && 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 { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index c39183242..cbe435f81 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -2550,11 +2550,19 @@ private: functionVariableUsage("void foo()\n" "{\n" - " string* txt = new string(\"test\");\n" + " Fred* fred = new Fred;\n" " std::cout << \"test\" << std::endl;\n" - " delete txt;\n" + " delete fred;\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"