From 16e55f4f8908acbc47b76ad7f89c73df2dabd8fe Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Thu, 24 Sep 2009 23:46:08 +0300 Subject: [PATCH] Improve C style pointer cast detection http://sourceforge.net/apps/trac/cppcheck/ticket/724 --- src/checkother.cpp | 3 ++- test/testother.cpp | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index 321300503..06cf94f0b 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -45,7 +45,8 @@ void CheckOther::warningOldStylePointerCast() for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Old style pointer casting.. - if (!Token::Match(tok, "( const| %type% * ) %var%")) + if (!Token::Match(tok, "( const| %type% * ) %var%") && + !Token::Match(tok, "( const| %type% * ) (| new")) continue; int addToIndex = 0; diff --git a/test/testother.cpp b/test/testother.cpp index 7a116b4e5..0e8e4649b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -782,6 +782,27 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str()); + checkOldStylePointerCast("class Base;\n" + "void foo()\n" + "{\n" + " Base * b = (const Base *) ( new Derived() );\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str()); + + checkOldStylePointerCast("class Base;\n" + "void foo()\n" + "{\n" + " Base * b = (const Base *) new Derived();\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str()); + + checkOldStylePointerCast("class Base;\n" + "void foo()\n" + "{\n" + " Base * b = (const Base *) new short[10];\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str()); + checkOldStylePointerCast("class B;\n" "class A\n" "{\n"