Fix #11621 FP functionConst when assigning init list (#4895)

* Fix #11621 FP functionConst when assigning init list

* Add comment

* Merge
This commit is contained in:
chrchr-github 2023-04-01 18:54:26 +02:00 committed by GitHub
parent cf280f84d4
commit 7503aca0e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -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() == "?") else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->astParent() && lhs->astParent()->str() == "?")
lhs = lhs->astParent()->astParent(); lhs = lhs->astParent()->astParent();
if (lhs->str() == "&") { if (lhs->str() == "&") {
lhs = lhs->previous(); const Token* const top = lhs->astTop();
if (lhs->isAssignmentOp() && lhs->previous()->variable()) { if (top->isAssignmentOp()) {
if (lhs->previous()->variable()->typeStartToken()->strAt(-1) != "const" && lhs->previous()->variable()->isPointer()) if (Token::simpleMatch(top->astOperand2(), "{")) // TODO: check usage in init list
return false; 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) } 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. // TODO: We could additionally check what is done with the elements to avoid false negatives. Here we just rely on "const" keyword being used.

View File

@ -6395,6 +6395,18 @@ private:
ASSERT_EQUALS("", errout.str()); 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() { void const_handleDefaultParameters() {
checkConst("struct Foo {\n" checkConst("struct Foo {\n"
" void foo1(int i, int j = 0) {\n" " void foo1(int i, int j = 0) {\n"