Fixed #5701 (FP for std::unordered_map::operator[]; there is no const version)

This commit is contained in:
Mark de Wever 2014-04-21 16:39:44 +02:00 committed by Daniel Marjamäki
parent 52e0e4453b
commit 6f3e3f5e2e
2 changed files with 20 additions and 2 deletions

View File

@ -1783,8 +1783,12 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool&
} else if (end->strAt(1) == "[") {
if (end->varId()) {
const Variable *var = end->variable();
if (var && Token::simpleMatch(var->typeStartToken(), "std :: map")) // operator[] changes a map
// The container contains the STL types whose operator[] is not a const.
// THIS ARRAY MUST BE ORDERED ALPHABETICALLY
static const char* const stl_containers [] = {
"map", "unordered_map"
};
if (var && var->isStlType(stl_containers))
return false;
}
if (!jumpBackToken)

View File

@ -147,6 +147,7 @@ private:
TEST_CASE(const59); // ticket #4646
TEST_CASE(const60); // ticket #3322
TEST_CASE(const61); // ticket #5606
TEST_CASE(const62); // ticket #5701
TEST_CASE(const_handleDefaultParameters);
TEST_CASE(const_passThisToMemberOfOtherClass);
TEST_CASE(assigningPointerToPointerIsNotAConstOperation);
@ -4828,6 +4829,19 @@ private:
ASSERT_EQUALS("", errout.str());
}
void const62() {
checkConst("class A {\n"
" private:\n"
" std::unordered_map<unsigned int,unsigned int> _hash;\n"
" public:\n"
" A() : _hash() {}\n"
" unsigned int fetch(unsigned int key)\n" // cannot be 'const'
" {\n"
" return _hash[key];\n"
" }\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void const_handleDefaultParameters() {
checkConst("struct Foo {\n"