Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class TestClass : public TestFixture {
TEST_CASE(const82); // ticket #11513
TEST_CASE(const83);
TEST_CASE(const84);
TEST_CASE(const85);

TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
Expand Down Expand Up @@ -6374,6 +6375,18 @@ class TestClass : public TestFixture {
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"
Expand Down