diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index d04630088..d558a52ac 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1630,7 +1630,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok) isconst = false; break; } - else if (Token::Match(tok1, "%var% . size ( )") && tok1->varId()) + else if (Token::Match(tok1, "%var% . size|empty ( )") && tok1->varId()) { // STL container size() is const static const char STL_CONTAINER_LIST[] = "bitset|deque|list|map|multimap|multiset|priority_queue|queue|set|stack|hash_map|hash_multimap|hash_set|string|vector"; diff --git a/test/testclass.cpp b/test/testclass.cpp index 79f3a55b9..e66c8a210 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -4808,7 +4808,6 @@ private: ); ASSERT_EQUALS("[test.cpp:4]: (information) Technically the member function 'A::strGetString1' can be const.\n", errout.str()); - checkConst("class A{\n" "public:\n" "A(){m_strVec.push_back("");}\n" @@ -4819,6 +4818,17 @@ private: "};\n" ); ASSERT_EQUALS("[test.cpp:4]: (information) Technically the member function 'A::strGetSize' can be const.\n", errout.str()); + + checkConst("class A{\n" + "public:\n" + "A(){m_strVec.push_back("");}\n" + "bool strGetEmpty()\n" + "{return m_strVec.empty();}\n" + "private:\n" + "std::vector m_strVec;\n" + "};\n" + ); + ASSERT_EQUALS("[test.cpp:4]: (information) Technically the member function 'A::strGetEmpty' can be const.\n", errout.str()); } void const26() // ticket #1847 @@ -5988,6 +5998,16 @@ private: " unsigned int GetVecSize() {return m_v.size();}\n" "};"); ASSERT_EQUALS("[test.cpp:7]: (information) Technically the member function 'A::GetVecSize' can be const.\n", errout.str()); + + checkConst("#include \n" + "class A\n" + "{\n" + " std::vector m_v;\n" + "public:\n" + " A(){}\n" + " bool GetVecEmpty() {return m_v.empty();}\n" + "};"); + ASSERT_EQUALS("[test.cpp:7]: (information) Technically the member function 'A::GetVecEmpty' can be const.\n", errout.str()); } void constVirtualFunc()