Fix FP unusedStructMember with std::map (#4899)

This commit is contained in:
chrchr-github 2023-03-20 19:40:57 +01:00 committed by GitHub
parent 3d965b5b81
commit 6316479782
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 7 deletions

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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<int, N::S> 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("");