Fix #11607 FP constVariable with auto and std::map (#4877)

This commit is contained in:
chrchr-github 2023-03-09 20:01:50 +01:00 committed by GitHub
parent 6e1cc11eff
commit c76b05ad75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 4 deletions

View File

@ -2281,7 +2281,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const
return false;
}
const std::set<std::string> CheckClass::stl_containers_not_const = { "map", "unordered_map" };
const std::set<std::string> CheckClass::stl_containers_not_const = { "map", "unordered_map", "std :: map|unordered_map <" }; // start pattern
bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, bool& memberAccessed) const
{

View File

@ -1526,10 +1526,9 @@ void CheckOther::checkConstVariable()
callNonConstMethod = true;
break;
}
if (var->isStlType() && Token::Match(tok, "%var% [")) { // containers whose operator[] is non-const
const Token* typeTok = var->typeStartToken() ? var->typeStartToken()->tokAt(2) : nullptr;
if (var->valueType() && var->valueType()->container && Token::Match(tok, "%var% [")) { // containers whose operator[] is non-const
const auto& notConst = CheckClass::stl_containers_not_const;
if (typeTok && notConst.find(typeTok->str()) != notConst.end()) {
if (notConst.find(var->valueType()->container->startPattern) != notConst.end()) {
callNonConstMethod = true;
break;
}

View File

@ -3023,6 +3023,12 @@ private:
"};\n");
ASSERT_EQUALS("", errout.str());
check("void f(std::vector<std::map<int, int>>& v) {\n" // #11607
" for (auto& m : v)\n"
" std::cout << m[0];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("struct S { int i; };\n" // #11473
"void f(std::vector<std::vector<S>>&m, int*& p) {\n"
" auto& a = m[0];\n"