Fix #11330 FN functionConst with access of smart pointer (#4720)

* Fix #11330 FNfunctionConst with access of smart pointer

* Simplify
This commit is contained in:
chrchr-github 2023-01-16 22:07:04 +01:00 committed by GitHub
parent 124668979c
commit 6e2cc450b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -2263,7 +2263,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const
{ {
if (!tok->function()) if (!tok->function())
return false; return false;
else if (tok->function()->nestedIn == scope) if (tok->function()->nestedIn == scope)
return tok->function()->isConst(); return tok->function()->isConst();
// not found in this class // not found in this class
@ -2390,7 +2390,9 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
&& (tok1->previous()->isComparisonOp() || && (tok1->previous()->isComparisonOp() ||
(tok1->previous()->isAssignmentOp() && tok1->tokAt(-2)->variable() && Token::Match(tok1->tokAt(-2)->variable()->typeEndToken(), "const_iterator|const_reverse_iterator"))))) (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; return false;
} }

View File

@ -197,6 +197,7 @@ private:
TEST_CASE(const78); // ticket #10315 TEST_CASE(const78); // ticket #10315
TEST_CASE(const79); // ticket #9861 TEST_CASE(const79); // ticket #9861
TEST_CASE(const80); // ticket #11328 TEST_CASE(const80); // ticket #11328
TEST_CASE(const81); // ticket #11330
TEST_CASE(const_handleDefaultParameters); TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass); TEST_CASE(const_passThisToMemberOfOtherClass);
TEST_CASE(assigningPointerToPointerIsNotAConstOperation); TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
@ -6203,6 +6204,20 @@ private:
errout.str()); errout.str());
} }
void const81() { // #11330
checkConst("struct A {\n"
" bool f() const;\n"
"};\n"
"struct S {\n"
" std::shared_ptr<A> 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() { void const_handleDefaultParameters() {
checkConst("struct Foo {\n" checkConst("struct Foo {\n"
" void foo1(int i, int j = 0) {\n" " void foo1(int i, int j = 0) {\n"