diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index ea274dc5f..880f18343 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2365,10 +2365,14 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->astParent() && lhs->astParent()->str() == "?") lhs = lhs->astParent()->astParent(); if (lhs->str() == "&") { - lhs = lhs->previous(); - if (lhs->isAssignmentOp() && lhs->previous()->variable()) { - if (lhs->previous()->variable()->typeStartToken()->strAt(-1) != "const" && lhs->previous()->variable()->isPointer()) + const Token* const top = lhs->astTop(); + if (top->isAssignmentOp()) { + if (Token::simpleMatch(top->astOperand2(), "{")) // TODO: check usage in init list return false; + else if (top->previous()->variable()) { + if (top->previous()->variable()->typeStartToken()->strAt(-1) != "const" && top->previous()->variable()->isPointer()) + return false; + } } } else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->str() == "(") { // range-based for-loop (C++11) // TODO: We could additionally check what is done with the elements to avoid false negatives. Here we just rely on "const" keyword being used. diff --git a/test/testclass.cpp b/test/testclass.cpp index 76d079c92..a198d1381 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -6395,6 +6395,18 @@ private: ASSERT_EQUALS("", errout.str()); } + void const85() { // #11621 + checkConst("struct S { int* p; };\n" + "struct T { int m; int* p; };\n" + "struct U {\n" + " int i;\n" + " void f() { S s = { &i }; }\n" + " void g() { int* a[] = { &i }; }\n" + " void h() { T t = { 1, &i }; }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void const_handleDefaultParameters() { checkConst("struct Foo {\n" " void foo1(int i, int j = 0) {\n"