@@ -1810,18 +1810,33 @@ void CheckOtherImpl::checkConstVariable()
18101810 if (function && (Function::returnsReference (function) || Function::returnsPointer (function)) && !Function::returnsConst (function)) {
18111811 std::vector<const Token*> returns = Function::findReturns (function);
18121812 if (std::any_of (returns.cbegin (), returns.cend (), [&](const Token* retTok) {
1813- if (retTok->varId () == var->declarationId ())
1814- return true ;
1815- while (retTok && retTok->isCast ())
1816- retTok = retTok->astOperand2 () ? retTok->astOperand2 () : retTok->astOperand1 ();
1817- while (Token::simpleMatch (retTok, " ." ))
1818- retTok = retTok->astOperand2 ();
1819- if (Token::simpleMatch (retTok, " &" ))
1820- retTok = retTok->astOperand1 ();
1821- ValueFlow::Value ltVal = ValueFlow::getLifetimeObjValue (retTok);
1822- if (ltVal.isLifetimeValue () && ltVal.tokvalue ->varId () == var->declarationId ())
1823- return true ;
1824- return ValueFlow::hasLifetimeToken (getParentLifetime (retTok), var->nameToken (), mSettings );
1813+ bool result = false ;
1814+ visitAstNodes (retTok, [&result, &var, this ](const Token* tok) {
1815+ if (tok->varId () == var->declarationId ()) {
1816+ result = true ;
1817+ return ChildrenToVisit::done;
1818+ }
1819+ if (tok->isCast ())
1820+ return tok->astOperand2 () ? ChildrenToVisit::op2 : ChildrenToVisit::op1;
1821+ if (tok->str () == " ." )
1822+ return ChildrenToVisit::op2;
1823+ if (tok->str () == " &" )
1824+ return ChildrenToVisit::op1;
1825+ if (tok->str () == " ?" )
1826+ return ChildrenToVisit::op2;
1827+ if (tok->str () == " :" )
1828+ return ChildrenToVisit::op1_and_op2;
1829+
1830+ ValueFlow::Value ltVal = ValueFlow::getLifetimeObjValue (tok);
1831+ if ((ltVal.isLifetimeValue () && ltVal.tokvalue ->varId () == var->declarationId ()) ||
1832+ ValueFlow::hasLifetimeToken (getParentLifetime (tok), var->nameToken (), mSettings )) {
1833+ result = true ;
1834+ return ChildrenToVisit::done;
1835+ }
1836+ return ChildrenToVisit::none;
1837+ });
1838+ return result;
1839+
18251840 }))
18261841 continue ;
18271842 }
0 commit comments