From a5b0fd38fdebe4f61aa38c4ac8710a9a4d6a3056 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 9 Mar 2023 20:07:44 +0100 Subject: [PATCH] Fix FP functionConst with ternary (#4874) --- lib/checkclass.cpp | 4 ++++ test/testclass.cpp | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index a4def1cbb2c..082a36b56da 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -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()) { diff --git a/test/testclass.cpp b/test/testclass.cpp index 7b8d9483506..7245d4b3af7 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -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); @@ -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"