diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6556ff204..6354859ba 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3508,6 +3508,21 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold valueFlowContainerForward(var->nameToken()->next(), var->declarationId(), value, settings); } + // after assignment + for (const Scope *functionScope : symboldatabase->functionScopes) { + for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) { + if (Token::Match(tok, "[;{}] %var% = %str% ;")) { + const Token *containerTok = tok->next(); + if (containerTok && containerTok->valueType() && containerTok->valueType()->container && containerTok->valueType()->container->stdStringLike) { + ValueFlow::Value value(Token::getStrLength(containerTok->tokAt(2))); + value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; + value.setKnown(); + valueFlowContainerForward(containerTok->next(), containerTok->varId(), value, settings); + } + } + } + } + // conditional conditionSize for (const Scope &scope : symboldatabase->scopeList) { if (scope.type != Scope::ScopeType::eIf) // TODO: while diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index f2b361cfb..8770fc6a0 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -3327,10 +3327,16 @@ private: ASSERT(tokenValues(code, "v [").empty()); code = "void f() {\n" - " std::list ints;\n" + " std::list ints;\n" // No value => ints is empty " ints.front();\n" "}"; ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 0)); + + code = "void f() {\n" + " std::string s=\"abc\";\n" // size of s is 3 + " s.size();\n" + "}"; + ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "s . size"), 3)); } };