From bc62472a18836cb45d23c3ef523c66fcf5ba0bc3 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Wed, 20 May 2009 01:23:43 +0700 Subject: [PATCH] Part of fix for ticket #284 (style check: redundant condition improvement) Fixed case "if (p != NULL) delete p;" and also added test case for it. http://apps.sourceforge.net/trac/cppcheck/ticket/284 --- src/checkother.cpp | 2 +- test/testother.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index fc5bde097..8ef7c59a7 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -82,7 +82,7 @@ void CheckOther::WarningRedundantCode() varname1 = tok->strAt(2); tok2 = tok->tokAt(4); } - else if (Token::Match(tok, "if ( %var% != NULL )")) + else if (Token::Match(tok, "if ( %var% != 0 )")) { varname1 = tok->strAt(2); tok2 = tok->tokAt(6); diff --git a/test/testother.cpp b/test/testother.cpp index 6b5381d76..89986865b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -145,6 +145,13 @@ private: " delete p;\n" "}\n"); ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str()); + + check("void foo()\n" + "{\n" + " if (p != NULL)\n" + " delete p;\n" + "}\n"); + ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str()); } void unreachable1()