Partial fix for #11637 FN functionConst (#5102)

* Partial fix for #11637 FN functionConst

* Redundant check
This commit is contained in:
chrchr-github 2023-05-31 20:55:12 +02:00 committed by GitHub
parent 0ecf101fe3
commit a91222fad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -2391,7 +2391,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
if (lhs->str() == "&") {
const Token* const top = lhs->astTop();
if (top->isAssignmentOp()) {
if (Token::simpleMatch(top->astOperand2(), "{")) // TODO: check usage in init list
if (Token::simpleMatch(top->astOperand2(), "{") && !top->astOperand2()->previous()->function()) // TODO: check usage in init list
return false;
else if (top->previous()->variable()) {
if (top->previous()->variable()->typeStartToken()->strAt(-1) != "const" && top->previous()->variable()->isPointer())

View File

@ -1102,7 +1102,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass)
// Set function call pointers
for (const Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
if (tok->isName() && !tok->function() && tok->varId() == 0 && Token::Match(tok, "%name% [(,)>;]") && !isReservedName(tok->str())) {
if (tok->isName() && !tok->function() && tok->varId() == 0 && Token::Match(tok, "%name% [{(,)>;]") && !isReservedName(tok->str())) {
if (tok->next()->str() == ">" && !tok->next()->link())
continue;

View File

@ -179,6 +179,7 @@ private:
TEST_CASE(const87);
TEST_CASE(const88);
TEST_CASE(const89);
TEST_CASE(const90);
TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
@ -6540,6 +6541,22 @@ private:
ASSERT_EQUALS("", errout.str());
}
void const90() { // #11637
checkConst("class S {};\n"
"struct C {\n"
" C(const S*);\n"
" C(const S&);\n"
"};\n"
"class T {\n"
" S s;\n"
" void f1() { C c = C{ &s }; }\n"
" void f2() { C c = C{ s }; }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:8]: (style, inconclusive) Technically the member function 'T::f1' can be const.\n"
"[test.cpp:9]: (style, inconclusive) Technically the member function 'T::f2' can be const.\n",
errout.str());
}
void const_handleDefaultParameters() {
checkConst("struct Foo {\n"
" void foo1(int i, int j = 0) {\n"