Skip to content

Commit

Permalink
Fix FP functionConst with ternary (danmar#4874)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Mar 9, 2023
1 parent 3db05cf commit a5b0fd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
18 changes: 18 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class TestClass : public TestFixture {
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);
Expand Down Expand Up @@ -6321,6 +6322,23 @@ class TestClass : public TestFixture {
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"
Expand Down

0 comments on commit a5b0fd3

Please sign in to comment.