From 6e2cc450b877893c2c3da7a5291db73d53f1d7c5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 16 Jan 2023 22:07:04 +0100 Subject: [PATCH] Fix #11330 FN functionConst with access of smart pointer (#4720) * Fix #11330 FNfunctionConst with access of smart pointer * Simplify --- lib/checkclass.cpp | 6 ++++-- test/testclass.cpp | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 5f7b4df5b..4cb706de0 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2263,7 +2263,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const { if (!tok->function()) return false; - else if (tok->function()->nestedIn == scope) + if (tok->function()->nestedIn == scope) return tok->function()->isConst(); // not found in this class @@ -2390,7 +2390,9 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& && (tok1->previous()->isComparisonOp() || (tok1->previous()->isAssignmentOp() && tok1->tokAt(-2)->variable() && Token::Match(tok1->tokAt(-2)->variable()->typeEndToken(), "const_iterator|const_reverse_iterator"))))) ; - else if (!var->typeScope() || !isConstMemberFunc(var->typeScope(), end)) + else if (var->smartPointerType() && var->smartPointerType()->classScope && isConstMemberFunc(var->smartPointerType()->classScope, end)) { + ; + } else if (!var->typeScope() || !isConstMemberFunc(var->typeScope(), end)) return false; } diff --git a/test/testclass.cpp b/test/testclass.cpp index cbdd47bb3..46e7d999f 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -197,6 +197,7 @@ private: TEST_CASE(const78); // ticket #10315 TEST_CASE(const79); // ticket #9861 TEST_CASE(const80); // ticket #11328 + TEST_CASE(const81); // ticket #11330 TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_passThisToMemberOfOtherClass); TEST_CASE(assigningPointerToPointerIsNotAConstOperation); @@ -6203,6 +6204,20 @@ private: errout.str()); } + void const81() { // #11330 + checkConst("struct A {\n" + " bool f() const;\n" + "};\n" + "struct S {\n" + " std::shared_ptr a;\n" + " void g() {\n" + " if (a->f()) {}\n" + " }\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:6]: (style, inconclusive) Technically the member function 'S::g' can be const.\n", + errout.str()); + } + void const_handleDefaultParameters() { checkConst("struct Foo {\n" " void foo1(int i, int j = 0) {\n"