diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 1b579d0ed..3757966ec 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2364,7 +2364,9 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& } // non const pointer cast - if (tok1->valueType() && tok1->valueType()->pointer > 0 && tok1->astParent() && tok1->astParent()->isCast() && !Token::simpleMatch(tok1->astParent(), "( const")) + if (tok1->valueType() && tok1->valueType()->pointer > 0 && tok1->astParent() && tok1->astParent()->isCast() && + !(tok1->astParent()->valueType() && + (tok1->astParent()->valueType()->pointer == 0 || tok1->astParent()->valueType()->isConst(tok1->astParent()->valueType()->pointer)))) return false; const Token* lhs = tok1->previous(); diff --git a/test/testclass.cpp b/test/testclass.cpp index 1a5cb02dc..f5a323d9e 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -197,6 +197,7 @@ private: TEST_CASE(const84); TEST_CASE(const85); TEST_CASE(const86); + TEST_CASE(const87); TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_passThisToMemberOfOtherClass); @@ -6398,6 +6399,21 @@ private: ASSERT_EQUALS("", errout.str()); } + void const87() { // #11626 + checkConst("struct S {\n" + " bool f() { return static_cast(p); }\n" + " const int* g() { return const_cast(p); }\n" + " const int* h() { return (const int*)p; }\n" + " char* j() { return reinterpret_cast(p); }\n" + " char* k() { return (char*)p; }\n" + " int* p;\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Technically the member function 'S::f' can be const.\n" + "[test.cpp:3]: (style, inconclusive) Technically the member function 'S::g' can be const.\n" + "[test.cpp:4]: (style, inconclusive) Technically the member function 'S::h' can be const.\n", + errout.str()); + } + void const_handleDefaultParameters() { checkConst("struct Foo {\n" " void foo1(int i, int j = 0) {\n"