From 0561e62308de5ce04591528acdb491143b8d15c9 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 24 Aug 2024 13:26:29 +0200 Subject: [PATCH] Fix #7459 False positive (inconclusive): function can be static --- lib/checkclass.cpp | 2 ++ test/testclass.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 8c525987793..ac88b46183b 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2111,6 +2111,8 @@ void CheckClass::checkConst() // don't warn for friend/static/virtual functions if (func.isFriend() || func.isStatic() || func.hasVirtualSpecifier()) continue; + if (func.functionPointerUsage) + continue; // don't suggest const when returning non-const pointer/reference, but still suggest static auto isPointerOrReference = [this](const Token* start, const Token* end) -> bool { diff --git a/test/testclass.cpp b/test/testclass.cpp index 130e4926d5d..2cab08eeee1 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -185,6 +185,7 @@ class TestClass : public TestFixture { TEST_CASE(const91); TEST_CASE(const92); TEST_CASE(const93); + TEST_CASE(const94); TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_passThisToMemberOfOtherClass); @@ -6691,6 +6692,19 @@ class TestClass : public TestFixture { errout_str()); } + void const94() { // #7459 + checkConst("class A {\n" + "public:\n" + " A() : tickFunction(&A::nop) {}\n" + " void tick() { (this->*tickFunction)(); }\n" + "private:\n" + " typedef void (A::* Fn)();\n" + " Fn tickFunction;\n" + " void nop() {}\n" + "};\n"); + ASSERT_EQUALS("", errout_str()); + } + void const_handleDefaultParameters() { checkConst("struct Foo {\n" " void foo1(int i, int j = 0) {\n"