@@ -1828,18 +1828,31 @@ void CheckOtherImpl::checkConstVariable()
18281828 if (function && (Function::returnsReference (function) || Function::returnsPointer (function)) && !Function::returnsConst (function)) {
18291829 std::vector<const Token*> returns = Function::findReturns (function);
18301830 if (std::any_of (returns.cbegin (), returns.cend (), [&](const Token* retTok) {
1831- if (retTok->varId () == var->declarationId ())
1832- return true ;
1833- while (retTok && retTok->isCast ())
1834- retTok = retTok->astOperand2 () ? retTok->astOperand2 () : retTok->astOperand1 ();
1835- while (Token::simpleMatch (retTok, " ." ))
1836- retTok = retTok->astOperand2 ();
1837- if (Token::simpleMatch (retTok, " &" ))
1838- retTok = retTok->astOperand1 ();
1839- ValueFlow::Value ltVal = ValueFlow::getLifetimeObjValue (retTok);
1840- if (ltVal.isLifetimeValue () && ltVal.tokvalue ->varId () == var->declarationId ())
1841- return true ;
1842- return ValueFlow::hasLifetimeToken (getParentLifetime (retTok), var->nameToken (), mSettings );
1831+ bool result = false ;
1832+ visitAstNodes (retTok, [&result, &var, this ](const Token* tok) {
1833+ if (tok->varId () == var->declarationId ()) {
1834+ result = true ;
1835+ return ChildrenToVisit::done;
1836+ }
1837+ ValueFlow::Value ltVal = ValueFlow::getLifetimeObjValue (tok);
1838+ if ((ltVal.isLifetimeValue () && ltVal.tokvalue ->varId () == var->declarationId ()) ||
1839+ ValueFlow::hasLifetimeToken (getParentLifetime (tok), var->nameToken (), mSettings )) {
1840+ result = true ;
1841+ return ChildrenToVisit::done;
1842+ }
1843+ if (tok->isCast ())
1844+ return tok->astOperand2 () ? ChildrenToVisit::op2 : ChildrenToVisit::op1;
1845+ if (tok->str () == " ." )
1846+ return ChildrenToVisit::op2;
1847+ if (tok->str () == " &" )
1848+ return ChildrenToVisit::op1;
1849+ if (tok->str () == " ?" )
1850+ return ChildrenToVisit::op2;
1851+ if (tok->str () == " :" )
1852+ return ChildrenToVisit::op1_and_op2;
1853+ return ChildrenToVisit::none;
1854+ });
1855+ return result;
18431856 }))
18441857 continue ;
18451858 }
0 commit comments