Skip to content

Commit a5b0fd3

Browse files
Fix FP functionConst with ternary (#4874)
1 parent 3db05cf commit a5b0fd3

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/checkclass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,10 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
23472347
const Token* lhs = tok1->previous();
23482348
if (lhs->str() == "(" && tok1->astParent() && tok1->astParent()->astParent())
23492349
lhs = tok1->astParent()->astParent();
2350+
else if (lhs->str() == "?" && lhs->astParent())
2351+
lhs = lhs->astParent();
2352+
else if (lhs->str() == ":" && lhs->astParent() && lhs->astParent()->astParent() && lhs->astParent()->str() == "?")
2353+
lhs = lhs->astParent()->astParent();
23502354
if (lhs->str() == "&") {
23512355
lhs = lhs->previous();
23522356
if (lhs->isAssignmentOp() && lhs->previous()->variable()) {

test/testclass.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class TestClass : public TestFixture {
195195
TEST_CASE(const79); // ticket #9861
196196
TEST_CASE(const80); // ticket #11328
197197
TEST_CASE(const81); // ticket #11330
198+
TEST_CASE(const82);
198199
TEST_CASE(const_handleDefaultParameters);
199200
TEST_CASE(const_passThisToMemberOfOtherClass);
200201
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
@@ -6321,6 +6322,23 @@ class TestClass : public TestFixture {
63216322
ASSERT_EQUALS("", errout.str());
63226323
}
63236324

6325+
void const82() {
6326+
checkConst("struct S {\n"
6327+
" int i1, i2;\n"
6328+
" void f(bool b);\n"
6329+
" void g(bool b, int j);\n"
6330+
"};\n"
6331+
"void S::f(bool b) {\n"
6332+
" int& r = b ? i1 : i2;\n"
6333+
" r = 5;\n"
6334+
"}\n"
6335+
"void S::g(bool b, int j) {\n"
6336+
" int& r = b ? j : i2;\n"
6337+
" r = 5;\n"
6338+
"}\n");
6339+
ASSERT_EQUALS("", errout.str());
6340+
}
6341+
63246342
void const_handleDefaultParameters() {
63256343
checkConst("struct Foo {\n"
63266344
" void foo1(int i, int j = 0) {\n"

0 commit comments

Comments
 (0)