Fix todo test for returning a dangling reference (#3284)

This commit is contained in:
Paul Fultz II 2021-06-04 10:15:39 -05:00 committed by GitHub
parent 6a193139dc
commit 95c872b1ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 5 deletions

View File

@ -30,6 +30,7 @@
#include "tokenize.h"
#include "valueflow.h"
#include <algorithm>
#include <list>
//---------------------------------------------------------------------------
@ -434,7 +435,14 @@ static int getPointerDepth(const Token *tok)
{
if (!tok)
return 0;
return tok->valueType() ? tok->valueType()->pointer : 0;
if (tok->valueType())
return tok->valueType()->pointer;
int n = 0;
std::pair<const Token*, const Token*> decl = Token::typeDecl(tok);
for (const Token* tok2 = decl.first; tok2 != decl.second; tok2 = tok2->next())
if (Token::simpleMatch(tok, "*"))
n++;
return n;
}
static bool isDeadTemporary(bool cpp, const Token* tok, const Token* expr, const Library* library)

View File

@ -34,9 +34,10 @@
#include <algorithm>
#include <cassert>
#include <cstring>
#include <limits>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <unordered_map>
//---------------------------------------------------------------------------
@ -6906,7 +6907,12 @@ ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Va
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)
const bool templateVar =
funcVar->scope() && funcVar->scope()->function && funcVar->scope()->function->templateDef;
if (type1 == type2)
return ValueType::MatchResult::SAME;
if (!templateVar && type1.find("auto") == std::string::npos && type2.find("auto") == std::string::npos &&
type1 != type2)
return ValueType::MatchResult::NOMATCH;
}
return res;

View File

@ -2896,6 +2896,8 @@ static bool isLifetimeBorrowed(const ValueType *vt, const ValueType *vtParent)
return false;
if (!vt)
return false;
if (vt->pointer > 0 && vt->pointer == vtParent->pointer)
return true;
if (vt->type != ValueType::UNKNOWN_TYPE && vtParent->type != ValueType::UNKNOWN_TYPE && vtParent->container == vt->container) {
if (vtParent->pointer > vt->pointer)
return true;

View File

@ -2146,9 +2146,8 @@ private:
" return get_default(m, k, &x);\n"
"}\n",
true);
TODO_ASSERT_EQUALS(
ASSERT_EQUALS(
"[test.cpp:9] -> [test.cpp:9] -> [test.cpp:8] -> [test.cpp:9]: (error, inconclusive) Returning pointer to local variable 'x' that will be invalid when returning.\n",
"",
errout.str());
check("std::vector<int> g();\n"