Skip to content

Commit 7f902c6

Browse files
committed
Fix #14888 FP functionConst with array to pointer decay
1 parent ac061da commit 7f902c6

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

lib/checkclass.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,9 +2608,12 @@ bool CheckClassImpl::checkConstFunc(const Scope *scope, const Function *func, Me
26082608
return false;
26092609
} else {
26102610
if (lhs->isAssignmentOp()) {
2611-
const Variable* lhsVar = lhs->previous()->variable();
2612-
if (lhsVar && !lhsVar->isConst() && lhsVar->isReference() && lhs == lhsVar->nameToken()->next())
2613-
return false;
2611+
if (const Variable* lhsVar = lhs->previous()->variable()) {
2612+
if (!lhsVar->isConst() && lhsVar->isReference() && lhs == lhsVar->nameToken()->next())
2613+
return false;
2614+
if (lhsVar->isPointer() && v->isArray() && !(lhsVar->valueType() && lhsVar->valueType()->isConst(/*indirect*/ 1)))
2615+
return false;
2616+
}
26142617
}
26152618
}
26162619

test/testclass.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class TestClass : public TestFixture {
196196
TEST_CASE(const99);
197197
TEST_CASE(const100);
198198
TEST_CASE(const101);
199+
TEST_CASE(const102);
199200

200201
TEST_CASE(const_handleDefaultParameters);
201202
TEST_CASE(const_passThisToMemberOfOtherClass);
@@ -6998,6 +6999,22 @@ class TestClass : public TestFixture {
69986999
ASSERT_EQUALS("", errout_str());
69997000
}
70007001

7002+
void const102() {
7003+
checkConst("struct S {\n" // #14888
7004+
" void f() {\n"
7005+
" int *p = a;\n"
7006+
" *p = 0;\n"
7007+
" }\n"
7008+
" void g() {\n"
7009+
" const int *p = a;\n"
7010+
" if (*p) {}\n"
7011+
" }\n"
7012+
" int a[3];\n"
7013+
"};\n");
7014+
ASSERT_EQUALS("[test.cpp:6:10]: (style, inconclusive) Technically the member function 'S::g' can be const. [functionConst]\n",
7015+
errout_str());
7016+
}
7017+
70017018
void const_handleDefaultParameters() {
70027019
checkConst("struct Foo {\n"
70037020
" void foo1(int i, int j = 0) {\n"

0 commit comments

Comments
 (0)