Fix issue 10087: false positive: error: Reference to temporary returned. (#3205)

This commit is contained in:
Paul Fultz II 2021-04-09 00:44:20 -05:00 committed by GitHub
parent 255f273c46
commit 5cf2f7e633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View File

@ -4818,11 +4818,12 @@ static std::string getTypeString(const Token *typeToken)
while (Token::Match(typeToken, ":: %name%")) {
ret += "::" + typeToken->strAt(1);
typeToken = typeToken->tokAt(2);
}
if (typeToken->str() == "<") {
for (const Token *tok = typeToken; tok != typeToken->link(); tok = tok->next())
ret += tok->str();
ret += ">";
typeToken = typeToken->link()->next();
}
}
return ret;
}
@ -6857,7 +6858,7 @@ ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Va
ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Variable *callVar, const Variable *funcVar)
{
ValueType::MatchResult res = ValueType::matchParameter(call, funcVar->valueType());
if (res == ValueType::MatchResult::SAME && callVar && call->container) {
if (callVar && ((res == ValueType::MatchResult::SAME && call->container) || res == ValueType::MatchResult::UNKNOWN)) {
const std::string type1 = getTypeString(callVar->typeStartToken());
const std::string type2 = getTypeString(funcVar->typeStartToken());
if (type1 != type2)

View File

@ -407,6 +407,7 @@ private:
TEST_CASE(findFunction40); // #10135
TEST_CASE(findFunction41); // #10202
TEST_CASE(findFunction42);
TEST_CASE(findFunction43); // #10087
TEST_CASE(findFunctionContainer);
TEST_CASE(findFunctionExternC);
TEST_CASE(findFunctionGlobalScope); // ::foo
@ -6425,6 +6426,34 @@ private:
ASSERT_EQUALS(2, functok->function()->tokenDef->linenr());
}
void findFunction43() { // #10087
{
GET_SYMBOL_DB("struct A {};\n"
"const A* g(const std::string&);\n"
"const A& g(std::vector<A>::size_type i);\n"
"const A& f(std::vector<A>::size_type i) { return g(i); }\n");
ASSERT_EQUALS("", errout.str());
const Token *functok = Token::findsimplematch(tokenizer.tokens(), "g ( i )");
ASSERT(functok);
ASSERT(functok->function());
ASSERT(functok->function()->name() == "g");
ASSERT_EQUALS(3, functok->function()->tokenDef->linenr());
}
{
GET_SYMBOL_DB("struct A {};\n"
"const A& g(std::vector<A>::size_type i);\n"
"const A* g(const std::string&);\n"
"const A& f(std::vector<A>::size_type i) { return g(i); }\n");
ASSERT_EQUALS("", errout.str());
const Token *functok = Token::findsimplematch(tokenizer.tokens(), "g ( i )");
ASSERT(functok);
ASSERT(functok->function());
ASSERT(functok->function()->name() == "g");
ASSERT_EQUALS(2, functok->function()->tokenDef->linenr());
}
}
void findFunctionContainer() {
{
GET_SYMBOL_DB("void dostuff(std::vector<int> v);\n"