Skip to content

Commit 6e2cc45

Browse files
Fix #11330 FN functionConst with access of smart pointer (#4720)
* Fix #11330 FNfunctionConst with access of smart pointer * Simplify
1 parent 1246689 commit 6e2cc45

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

lib/checkclass.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const
22632263
{
22642264
if (!tok->function())
22652265
return false;
2266-
else if (tok->function()->nestedIn == scope)
2266+
if (tok->function()->nestedIn == scope)
22672267
return tok->function()->isConst();
22682268

22692269
// not found in this class
@@ -2390,7 +2390,9 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
23902390
&& (tok1->previous()->isComparisonOp() ||
23912391
(tok1->previous()->isAssignmentOp() && tok1->tokAt(-2)->variable() && Token::Match(tok1->tokAt(-2)->variable()->typeEndToken(), "const_iterator|const_reverse_iterator")))))
23922392
;
2393-
else if (!var->typeScope() || !isConstMemberFunc(var->typeScope(), end))
2393+
else if (var->smartPointerType() && var->smartPointerType()->classScope && isConstMemberFunc(var->smartPointerType()->classScope, end)) {
2394+
;
2395+
} else if (!var->typeScope() || !isConstMemberFunc(var->typeScope(), end))
23942396
return false;
23952397
}
23962398

test/testclass.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ class TestClass : public TestFixture {
197197
TEST_CASE(const78); // ticket #10315
198198
TEST_CASE(const79); // ticket #9861
199199
TEST_CASE(const80); // ticket #11328
200+
TEST_CASE(const81); // ticket #11330
200201
TEST_CASE(const_handleDefaultParameters);
201202
TEST_CASE(const_passThisToMemberOfOtherClass);
202203
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
@@ -6203,6 +6204,20 @@ class TestClass : public TestFixture {
62036204
errout.str());
62046205
}
62056206

6207+
void const81() { // #11330
6208+
checkConst("struct A {\n"
6209+
" bool f() const;\n"
6210+
"};\n"
6211+
"struct S {\n"
6212+
" std::shared_ptr<A> a;\n"
6213+
" void g() {\n"
6214+
" if (a->f()) {}\n"
6215+
" }\n"
6216+
"};\n");
6217+
ASSERT_EQUALS("[test.cpp:6]: (style, inconclusive) Technically the member function 'S::g' can be const.\n",
6218+
errout.str());
6219+
}
6220+
62066221
void const_handleDefaultParameters() {
62076222
checkConst("struct Foo {\n"
62086223
" void foo1(int i, int j = 0) {\n"

0 commit comments

Comments
 (0)