diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index b610e963d76..b53b22170e9 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2608,9 +2608,12 @@ bool CheckClassImpl::checkConstFunc(const Scope *scope, const Function *func, Me return false; } else { if (lhs->isAssignmentOp()) { - const Variable* lhsVar = lhs->previous()->variable(); - if (lhsVar && !lhsVar->isConst() && lhsVar->isReference() && lhs == lhsVar->nameToken()->next()) - return false; + if (const Variable* lhsVar = lhs->previous()->variable()) { + if (!lhsVar->isConst() && lhsVar->isReference() && lhs == lhsVar->nameToken()->next()) + return false; + if (lhsVar->isPointer() && (v && v->isArray()) && !(lhsVar->valueType() && lhsVar->valueType()->isConst(/*indirect*/ 1))) + return false; + } } } diff --git a/test/testclass.cpp b/test/testclass.cpp index ae5068dcb9d..1d647eb33a1 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -196,6 +196,7 @@ class TestClass : public TestFixture { TEST_CASE(const99); TEST_CASE(const100); TEST_CASE(const101); + TEST_CASE(const102); TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_passThisToMemberOfOtherClass); @@ -6998,6 +6999,22 @@ class TestClass : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void const102() { + checkConst("struct S {\n" // #14888 + " void f() {\n" + " int *p = a;\n" + " *p = 0;\n" + " }\n" + " void g() {\n" + " const int *p = a;\n" + " if (*p) {}\n" + " }\n" + " int a[3];\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:6:10]: (style, inconclusive) Technically the member function 'S::g' can be const. [functionConst]\n", + errout_str()); + } + void const_handleDefaultParameters() { checkConst("struct Foo {\n" " void foo1(int i, int j = 0) {\n"