fix #1593 (false negative: the function can be declared as const)

This commit is contained in:
Robert Reif 2011-07-30 08:48:11 -04:00
parent 2f4aee2923
commit 74c1bdde77
2 changed files with 19 additions and 4 deletions

View File

@ -1630,6 +1630,23 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok)
isconst = false;
break;
}
else if (Token::Match(tok1, "%var% . size ( )") && 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";
const Variable *var = symbolDatabase->getVariableFromVarId(tok1->varId());
if (var)
{
const Token *tok2 = var->typeStartToken();
// skip namespace if present
if (Token::simpleMatch(tok2, "std ::"))
tok2 = tok2->tokAt(2);
// check for STL container
if (Token::Match(tok2, STL_CONTAINER_LIST))
tok1 = tok1->tokAt(4);
}
}
// delete..
else if (tok1->str() == "delete")

View File

@ -4818,8 +4818,7 @@ private:
"std::vector<std::string> m_strVec;\n"
"};\n"
);
TODO_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());
}
void const26() // ticket #1847
@ -5988,8 +5987,7 @@ private:
" A(){}\n"
" unsigned int GetVecSize() {return m_v.size();}\n"
"};");
TODO_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());
}
void constVirtualFunc()