Partial fix for #12162 false negative: functionConst (#5639)

This commit is contained in:
chrchr-github 2023-11-08 22:37:45 +01:00 committed by GitHub
parent 8f432880fa
commit 30bf5cac0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -2531,8 +2531,10 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
;
} else if (hasOverloadedMemberAccess(end, var->typeScope())) {
;
} else if (!var->typeScope() || (end->function() != func && !isConstMemberFunc(var->typeScope(), end)))
return false;
} else if (!var->typeScope() || (end->function() != func && !isConstMemberFunc(var->typeScope(), end))) {
if (!mSettings->library.isFunctionConst(end))
return false;
}
}
// Assignment

View File

@ -181,6 +181,7 @@ private:
TEST_CASE(const90);
TEST_CASE(const91);
TEST_CASE(const92);
TEST_CASE(const93);
TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
@ -6666,6 +6667,21 @@ private:
// don't hang
}
void const93() { // #12162
checkConst("struct S {\n"
" bool f() {\n"
" return m.cbegin()->first == 0;\n"
" }\n"
" bool g() {\n"
" return m.count(0);\n"
" }\n"
" std::map<int, int> m;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (style, inconclusive) Technically the member function 'S::f' can be const.\n"
"[test.cpp:5]: (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"