From 6316479782333ce69a9fedffe3e026e556faadf4 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 20 Mar 2023 19:40:57 +0100 Subject: [PATCH] Fix FP unusedStructMember with std::map (#4899) --- lib/library.cpp | 3 +++ lib/symboldatabase.cpp | 13 ++++++------- test/testunusedvar.cpp | 12 ++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/library.cpp b/lib/library.cpp index 01c4129b7..007834802 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1226,6 +1226,9 @@ bool Library::isContainerYield(const Token * const cond, Library::Container::Yie // returns true if ftok is not a library function bool Library::isNotLibraryFunction(const Token *ftok) const { + if (ftok->isKeyword() || ftok->isStandardType()) + return true; + if (ftok->function() && ftok->function()->nestedIn && ftok->function()->nestedIn->type != Scope::eGlobal) return true; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 4e24cc674..2c63d4200 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1278,13 +1278,12 @@ void SymbolDatabase::createSymbolDatabaseSetVariablePointers() const Variable *membervar = classScope ? classScope->getVariable(membertok->str()) : nullptr; setMemberVar(membervar, membertok, tok); } else if (tok->valueType() && tok->valueType()->type == ValueType::CONTAINER) { - if (Token::Match(var->typeStartToken(), "std :: %type% < %name%")) { - const Token* type2tok = var->typeStartToken()->tokAt(4); - while (type2tok && type2tok->isKeyword()) - type2tok = type2tok->next(); - const Type* type2 = type2tok ? type2tok->type() : nullptr; - if (type2 && type2->classScope && type2->classScope->definedType) { - const Variable *membervar = type2->classScope->getVariable(membertok->str()); + if (const Token* ctt = tok->valueType()->containerTypeToken) { + while (ctt && ctt->isKeyword()) + ctt = ctt->next(); + const Type* ct = findTypeInNested(ctt, tok->scope()); + if (ct && ct->classScope && ct->classScope->definedType) { + const Variable *membervar = ct->classScope->getVariable(membertok->str()); setMemberVar(membervar, membertok, tok); } } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index e7d87f1bb..22e104f90 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -69,6 +69,7 @@ private: TEST_CASE(structmember20); // #10737 TEST_CASE(structmember21); // #4759 TEST_CASE(structmember22); // #11016 + TEST_CASE(structmember23); TEST_CASE(localvar1); TEST_CASE(localvar2); @@ -1846,6 +1847,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void structmember23() { + checkStructMemberUsage("namespace N {\n" + " struct S { std::string s; };\n" + "}\n" + "std::string f() {\n" + " std::map m = { { 0, { \"abc\" } } };\n" + " return m[0].s;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") { // Clear the error buffer.. errout.str("");