fix #2795 (hash operator[] is not 'const') (original patch by Sebastien Debrard) (modified to use the symbol database)

This commit is contained in:
Robert Reif 2011-06-03 22:00:27 -04:00
parent a7fe9a3b50
commit 0f6da27b9f
2 changed files with 29 additions and 0 deletions

View File

@ -1598,6 +1598,19 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok)
}
}
// std::map variable member
else if (Token::Match(tok1, "%var% [") && isMemberVar(scope, tok1))
{
const Variable *var = symbolDatabase->getVariableFromVarId(tok1->varId());
if (var && (var->typeStartToken()->str() == "map" ||
Token::simpleMatch(var->typeStartToken(), "std :: map")))
{
isconst = false;
break;
}
}
// function call..
else if (Token::Match(tok1, "%var% (") &&
!(Token::Match(tok1, "return|c_str|if|string") || tok1->isStandardType()))

View File

@ -173,6 +173,7 @@ private:
TEST_CASE(const46); // ticket #2636
TEST_CASE(const47); // ticket #2670
TEST_CASE(const48); // ticket #2672
TEST_CASE(const49); // ticket #2795
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
TEST_CASE(assigningArrayElementIsNotAConstOperation);
TEST_CASE(constoperator1); // operator< can often be const
@ -5501,6 +5502,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void const49() // ticket 2795
{
checkConst("class A {\n"
" private:\n"
" std::map<unsigned int,unsigned int> _hash;\n"
" public:\n"
" A() : _hash() {}\n"
" unsigned int fetch(unsigned int key) // cannot be 'const'\n"
" {\n"
" return _hash[key];\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void assigningPointerToPointerIsNotAConstOperation()
{
checkConst("struct s\n"