add std::*.empty() to possible const functions in CheckClass::checkConstFunc

This commit is contained in:
Robert Reif 2011-07-30 09:44:20 -04:00
parent 74c1bdde77
commit c6a804d306
2 changed files with 22 additions and 2 deletions

View File

@ -1630,7 +1630,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok)
isconst = false; isconst = false;
break; 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 // 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"; 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";

View File

@ -4808,7 +4808,6 @@ private:
); );
ASSERT_EQUALS("[test.cpp:4]: (information) Technically the member function 'A::strGetString1' can be const.\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (information) Technically the member function 'A::strGetString1' can be const.\n", errout.str());
checkConst("class A{\n" checkConst("class A{\n"
"public:\n" "public:\n"
"A(){m_strVec.push_back("");}\n" "A(){m_strVec.push_back("");}\n"
@ -4819,6 +4818,17 @@ private:
"};\n" "};\n"
); );
ASSERT_EQUALS("[test.cpp:4]: (information) Technically the member function 'A::strGetSize' can be const.\n", errout.str()); 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<std::string> 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 void const26() // ticket #1847
@ -5988,6 +5998,16 @@ private:
" unsigned int GetVecSize() {return m_v.size();}\n" " 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()); ASSERT_EQUALS("[test.cpp:7]: (information) Technically the member function 'A::GetVecSize' can be const.\n", errout.str());
checkConst("#include <vector>\n"
"class A\n"
"{\n"
" std::vector<int> 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() void constVirtualFunc()