diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 259e991f7..7560e2b4f 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -8507,6 +8507,21 @@ static void valueFlowContainerSize(TokenList* tokenlist, value.setKnown(); valueFlowForward(containerTok->next(), containerTok, value, tokenlist, settings); } + } else if (Token::Match(tok->previous(), ">|return (|{") && astIsContainer(tok)) { + std::vector values; + if (Token::simpleMatch(tok, "{")) { + values = getInitListSize(tok, tok->valueType(), settings, true); + ValueFlow::Value value; + value.valueType = ValueFlow::Value::ValueType::TOK; + value.tokvalue = tok; + value.setKnown(); + values.push_back(value); + } else if (Token::simpleMatch(tok, "(")) { + const Token* constructorArgs = tok; + values = getContainerSizeFromConstructor(constructorArgs, tok->valueType(), settings, true); + } + for (const ValueFlow::Value& value : values) + setTokenValue(tok, value, settings); } else if (Token::Match(tok, "%name%|;|{|}|> %var% = {") && Token::simpleMatch(tok->linkAt(3), "} ;")) { Token* containerTok = tok->next(); if (containerTok->exprId() == 0) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 5e54f35d3..af178366f 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6481,6 +6481,15 @@ private: " return x;\n" "}\n"; ASSERT_EQUALS(true, testValueOfXKnown(code, 4U, 0)); + + code = "std::vector f() { return std::vector(); }"; + ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "( ) ;"), 0)); + + code = "std::vector f() { return std::vector{}; }"; + TODO_ASSERT_EQUALS("", "values.size():0", isKnownContainerSizeValue(tokenValues(code, "{ } ;"), 0)); + + code = "std::vector f() { return {}; }"; + ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "{ } ;"), 0)); } void valueFlowContainerElement()