From 7c3fbd7060a118453d2552214e49cc9adc500ab1 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sat, 20 Dec 2008 22:24:11 +0000 Subject: [PATCH] Fixed bug with redundant condition: http://sourceforge.net/forum/forum.php?thread_id=2711792&forum_id=693501 --- checkother.cpp | 47 ++++++++++++++++++++++++++++++++++++++--------- testother.cpp | 25 +++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/checkother.cpp b/checkother.cpp index 07ef9829d..c83e022e3 100644 --- a/checkother.cpp +++ b/checkother.cpp @@ -98,18 +98,47 @@ void CheckOther::WarningRedundantCode() if (varname1==NULL || tok2==NULL) continue; + bool err = false; if ( tok2->str() == "{" ) + { tok2 = tok2->next(); - bool err = false; - if (TOKEN::Match(tok2,"delete %var% ;")) - err = (strcmp(tok2->strAt(1),varname1)==0); - else if (TOKEN::Match(tok2,"delete [ ] %var% ;")) - err = (strcmp(tok2->strAt(1),varname1)==0); - else if (TOKEN::Match(tok2,"free ( %var% )")) - err = (strcmp(tok2->strAt(2),varname1)==0); - else if (TOKEN::Match(tok2,"kfree ( %var% )")) - err = (strcmp(tok2->strAt(2),varname1)==0); + if (TOKEN::Match(tok2,"delete %var% ; }")) + { + err = (strcmp(tok2->strAt(1),varname1)==0); + } + else if (TOKEN::Match(tok2,"delete [ ] %var% ; }")) + { + err = (strcmp(tok2->strAt(1),varname1)==0); + } + else if (TOKEN::Match(tok2,"free ( %var% ) ; }")) + { + err = (strcmp(tok2->strAt(2),varname1)==0); + } + else if (TOKEN::Match(tok2,"kfree ( %var% ) ; }")) + { + err = (strcmp(tok2->strAt(2),varname1)==0); + } + } + else + { + if (TOKEN::Match(tok2,"delete %var% ;")) + { + err = (strcmp(tok2->strAt(1),varname1)==0); + } + else if (TOKEN::Match(tok2,"delete [ ] %var% ;")) + { + err = (strcmp(tok2->strAt(1),varname1)==0); + } + else if (TOKEN::Match(tok2,"free ( %var% ) ;")) + { + err = (strcmp(tok2->strAt(2),varname1)==0); + } + else if (TOKEN::Match(tok2,"kfree ( %var% ) ;")) + { + err = (strcmp(tok2->strAt(2),varname1)==0); + } + } if (err) { diff --git a/testother.cpp b/testother.cpp index 110609f85..e0c86c06d 100644 --- a/testother.cpp +++ b/testother.cpp @@ -34,7 +34,9 @@ private: void run() { - // TODO TEST_CASE( delete1 ); + TEST_CASE( delete1 ); + + TEST_CASE( delete2 ); } void check( const char code[] ) @@ -59,11 +61,30 @@ private: " if (p)\n" " {\n" " delete p;\n" - " abc = 123;\n" + " p = 0;\n" " }\n" "}\n" ); ASSERT_EQUALS( std::string(""), errout.str() ); } + + void delete2() + { + check( "void foo()\n" + "{\n" + " if (p)\n" + " {\n" + " delete p;\n" + " }\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:3]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str() ); + + check( "void foo()\n" + "{\n" + " if (p)\n" + " delete p;\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:3]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str() ); + } }; REGISTER_TEST( TestOther )