Fix FP functionConst with ternary (#4874)

This commit is contained in:
chrchr-github 2023-03-09 20:07:44 +01:00 committed by GitHub
parent 3db05cf3a4
commit a5b0fd38fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -2347,6 +2347,10 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
const Token* lhs = tok1->previous();
if (lhs->str() == "(" && tok1->astParent() && tok1->astParent()->astParent())
lhs = tok1->astParent()->astParent();
else if (lhs->str() == "?" && lhs->astParent())
lhs = lhs->astParent();
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()) {

View File

@ -195,6 +195,7 @@ private:
TEST_CASE(const79); // ticket #9861
TEST_CASE(const80); // ticket #11328
TEST_CASE(const81); // ticket #11330
TEST_CASE(const82);
TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
@ -6321,6 +6322,23 @@ private:
ASSERT_EQUALS("", errout.str());
}
void const82() {
checkConst("struct S {\n"
" int i1, i2;\n"
" void f(bool b);\n"
" void g(bool b, int j);\n"
"};\n"
"void S::f(bool b) {\n"
" int& r = b ? i1 : i2;\n"
" r = 5;\n"
"}\n"
"void S::g(bool b, int j) {\n"
" int& r = b ? j : i2;\n"
" r = 5;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void const_handleDefaultParameters() {
checkConst("struct Foo {\n"
" void foo1(int i, int j = 0) {\n"