@@ -2718,30 +2718,6 @@ static void valueFlowForward(Token* startToken,
27182718 }
27192719}
27202720
2721- static std::vector<const Token*> findReturns (const Function* f)
2722- {
2723- std::vector<const Token*> result;
2724- const Scope* scope = f->functionScope ;
2725- if (!scope)
2726- return result;
2727- for (const Token* tok = scope->bodyStart ->next (); tok && tok != scope->bodyEnd ; tok = tok->next ()) {
2728- if (tok->str () == " {" && tok->scope () &&
2729- (tok->scope ()->type == Scope::eLambda || tok->scope ()->type == Scope::eClass)) {
2730- tok = tok->link ();
2731- continue ;
2732- }
2733- if (Token::simpleMatch (tok->astParent (), " return" )) {
2734- result.push_back (tok);
2735- }
2736- // Skip lambda functions since the scope may not be set correctly
2737- const Token* lambdaEndToken = findLambdaEndToken (tok);
2738- if (lambdaEndToken) {
2739- tok = lambdaEndToken;
2740- }
2741- }
2742- return result;
2743- }
2744-
27452721static int getArgumentPos (const Variable *var, const Function *f)
27462722{
27472723 auto arg_it = std::find_if (f->argumentList .begin (), f->argumentList .end (), [&](const Variable &v) {
@@ -2850,6 +2826,16 @@ std::vector<LifetimeToken> getLifetimeTokens(const Token* tok, ValueFlow::Value:
28502826 return {{tok, true , std::move (errorPath)}};
28512827 if (vartok)
28522828 return getLifetimeTokens (vartok, std::move (errorPath), depth - 1 );
2829+ } else if (Token::simpleMatch (var->nameToken ()->astParent (), " :" ) &&
2830+ var->nameToken ()->astParent ()->astParent () &&
2831+ Token::simpleMatch (var->nameToken ()->astParent ()->astParent ()->previous (), " for (" )) {
2832+ errorPath.emplace_back (var->nameToken (), " Assigned to reference." );
2833+ const Token* vartok = var->nameToken ();
2834+ if (vartok == tok)
2835+ return {{tok, true , std::move (errorPath)}};
2836+ const Token* contok = var->nameToken ()->astParent ()->astOperand2 ();
2837+ if (contok)
2838+ return getLifetimeTokens (contok, std::move (errorPath), depth - 1 );
28532839 } else {
28542840 return std::vector<LifetimeToken> {};
28552841 }
@@ -2860,7 +2846,7 @@ std::vector<LifetimeToken> getLifetimeTokens(const Token* tok, ValueFlow::Value:
28602846 if (!Function::returnsReference (f))
28612847 return {{tok, std::move (errorPath)}};
28622848 std::vector<LifetimeToken> result;
2863- std::vector<const Token*> returns = findReturns (f);
2849+ std::vector<const Token*> returns = Function:: findReturns (f);
28642850 for (const Token* returnTok : returns) {
28652851 if (returnTok == tok)
28662852 continue ;
@@ -2947,6 +2933,12 @@ const Variable* getLifetimeVariable(const Token* tok, ValueFlow::Value::ErrorPat
29472933 return nullptr ;
29482934}
29492935
2936+ const Variable* getLifetimeVariable (const Token* tok)
2937+ {
2938+ ValueFlow::Value::ErrorPath errorPath;
2939+ return getLifetimeVariable (tok, errorPath, nullptr );
2940+ }
2941+
29502942static bool isNotLifetimeValue (const ValueFlow::Value& val)
29512943{
29522944 return !val.isLifetimeValue ();
@@ -3394,7 +3386,7 @@ static void valueFlowLifetimeFunction(Token *tok, TokenList *tokenlist, ErrorLog
33943386 const Function *f = tok->function ();
33953387 if (Function::returnsReference (f))
33963388 return ;
3397- std::vector<const Token*> returns = findReturns (f);
3389+ std::vector<const Token*> returns = Function:: findReturns (f);
33983390 const bool inconclusive = returns.size () > 1 ;
33993391 for (const Token* returnTok : returns) {
34003392 if (returnTok == tok)
0 commit comments