diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 85fd349a6..4f4757fb4 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -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()) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index ef239d2c5..c131d4734 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -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; diff --git a/test/testclass.cpp b/test/testclass.cpp index bbef69603..6c12a4998 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -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"