diff --git a/src/checkother.cpp b/src/checkother.cpp index a69b30cef..3d5634c23 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -45,14 +45,18 @@ void CheckOther::warningOldStylePointerCast() for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Old style pointer casting.. - if (!Token::Match(tok, "( %type% * ) %var%")) + if (!Token::Match(tok, "( const| %type% * ) %var%")) continue; - if (Token::simpleMatch(tok->tokAt(4), "const")) + int addToIndex = 0; + if (Token::simpleMatch(tok->tokAt(1), "const")) + addToIndex = 1; + + if (Token::simpleMatch(tok->tokAt(4 + addToIndex), "const")) continue; // Is "type" a class? - const std::string pattern("class " + tok->next()->str()); + const std::string pattern("class " + tok->tokAt(1 + addToIndex)->str()); if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str())) continue; diff --git a/test/testother.cpp b/test/testother.cpp index 75ae08ff7..81ff4397b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -517,6 +517,12 @@ 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 *) derived;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n", errout.str()); checkOldStylePointerCast("class B;\n" "class A\n" @@ -524,6 +530,13 @@ private: " virtual void abc(B *) const = 0;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkOldStylePointerCast("class B;\n" + "class A\n" + "{\n" + " virtual void abc(const B *) const = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } };