Skip to content

Commit 7503aca

Browse files
Fix #11621 FP functionConst when assigning init list (#4895)
* Fix #11621 FP functionConst when assigning init list * Add comment * Merge
1 parent cf280f8 commit 7503aca

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

lib/checkclass.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,10 +2365,14 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
23652365
else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->astParent() && lhs->astParent()->str() == "?")
23662366
lhs = lhs->astParent()->astParent();
23672367
if (lhs->str() == "&") {
2368-
lhs = lhs->previous();
2369-
if (lhs->isAssignmentOp() && lhs->previous()->variable()) {
2370-
if (lhs->previous()->variable()->typeStartToken()->strAt(-1) != "const" && lhs->previous()->variable()->isPointer())
2368+
const Token* const top = lhs->astTop();
2369+
if (top->isAssignmentOp()) {
2370+
if (Token::simpleMatch(top->astOperand2(), "{")) // TODO: check usage in init list
23712371
return false;
2372+
else if (top->previous()->variable()) {
2373+
if (top->previous()->variable()->typeStartToken()->strAt(-1) != "const" && top->previous()->variable()->isPointer())
2374+
return false;
2375+
}
23722376
}
23732377
} else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->str() == "(") { // range-based for-loop (C++11)
23742378
// TODO: We could additionally check what is done with the elements to avoid false negatives. Here we just rely on "const" keyword being used.

test/testclass.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6395,6 +6395,18 @@ class TestClass : public TestFixture {
63956395
ASSERT_EQUALS("", errout.str());
63966396
}
63976397

6398+
void const85() { // #11621
6399+
checkConst("struct S { int* p; };\n"
6400+
"struct T { int m; int* p; };\n"
6401+
"struct U {\n"
6402+
" int i;\n"
6403+
" void f() { S s = { &i }; }\n"
6404+
" void g() { int* a[] = { &i }; }\n"
6405+
" void h() { T t = { 1, &i }; }\n"
6406+
"}\n");
6407+
ASSERT_EQUALS("", errout.str());
6408+
}
6409+
63986410
void const_handleDefaultParameters() {
63996411
checkConst("struct Foo {\n"
64006412
" void foo1(int i, int j = 0) {\n"

0 commit comments

Comments
 (0)