From c76b05ad75cb59a875063c38cebe531e32628645 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 9 Mar 2023 20:01:50 +0100 Subject: [PATCH] Fix #11607 FP constVariable with auto and std::map (#4877) --- lib/checkclass.cpp | 2 +- lib/checkother.cpp | 5 ++--- test/testother.cpp | 6 ++++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index e90207eef..a4def1cbb 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -2281,7 +2281,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const return false; } -const std::set CheckClass::stl_containers_not_const = { "map", "unordered_map" }; +const std::set 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 { diff --git a/lib/checkother.cpp b/lib/checkother.cpp index ae3d33a15..156006b6f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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; } diff --git a/test/testother.cpp b/test/testother.cpp index 31c6e6dd5..1db00d6d9 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3023,6 +3023,12 @@ private: "};\n"); ASSERT_EQUALS("", errout.str()); + check("void f(std::vector>& 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>&m, int*& p) {\n" " auto& a = m[0];\n"