@@ -4984,6 +4984,38 @@ static bool hasForLoopExit(const Scope& scope, const Library& library)
49844984 return false ;
49854985}
49864986
4987+ static bool boundDependsOnIndirectOrNonLocal (const Token* boundTok)
4988+ {
4989+ return findAstNode (boundTok, [](const Token* tok) {
4990+ if (tok->isUnaryOp (" *" ) || tok->str () == " [" )
4991+ return true ;
4992+ const Variable* var = tok->variable ();
4993+ if (!var)
4994+ return false ;
4995+ if (var->isReference () || var->isRValueReference ())
4996+ return true ;
4997+ // Global/static/member may be changed by an opaque call in the body
4998+ return !var->isLocal () && !var->isArgument ();
4999+ }) != nullptr ;
5000+ }
5001+
5002+ static bool rangeHasNonConstCall (const Token* start, const Token* end, const Library& library)
5003+ {
5004+ if (!start || !end)
5005+ return false ;
5006+ for (const Token* tok = start; tok != end; tok = tok->next ()) {
5007+ if (const Token* lambdaEnd = findLambdaEndToken (tok)) {
5008+ tok = lambdaEnd;
5009+ continue ;
5010+ }
5011+ if (!tok->isName () || !Token::simpleMatch (tok->next (), " (" ) || tok->isKeyword ())
5012+ continue ;
5013+ if (!isConstFunctionCall (tok, library))
5014+ return true ;
5015+ }
5016+ return false ;
5017+ }
5018+
49875019static bool isStableLoopBound (const Token* boundTok,
49885020 const Token* bodyStart,
49895021 const Token* bodyEnd,
@@ -5016,6 +5048,12 @@ static bool isStableLoopBound(const Token* boundTok,
50165048 return false ;
50175049 }
50185050
5051+ // Pointer/reference/non-local bound may be changed by a non-const call in the body
5052+ // (e.g. i < *p with shrink() updating the pointee; i < lim with lim a const int&).
5053+ if (boundDependsOnIndirectOrNonLocal (boundTok) &&
5054+ rangeHasNonConstCall (bodyStart, bodyEnd, settings.library ))
5055+ return false ;
5056+
50195057 return true ;
50205058}
50215059
@@ -5058,19 +5096,6 @@ static bool addressOrRefTakenBefore(const Variable* var, const Token* before)
50585096 return false ;
50595097}
50605098
5061- static bool loopBodyHasCall (const Scope& scope)
5062- {
5063- for (const Token* tok = scope.bodyStart ; tok != scope.bodyEnd ; tok = tok->next ()) {
5064- if (const Token* lambdaEnd = findLambdaEndToken (tok)) {
5065- tok = lambdaEnd;
5066- continue ;
5067- }
5068- if (tok->isName () && Token::simpleMatch (tok->next (), " (" ) && !tok->isKeyword ())
5069- return true ;
5070- }
5071- return false ;
5072- }
5073-
50745099static const Token* findForLoopControlVar (const Token* initTok, const Token* condTok)
50755100{
50765101 if (!initTok || !condTok || !condTok->isComparisonOp ())
@@ -5133,9 +5158,9 @@ void CheckOtherImpl::checkMissingForLoopIncrement()
51335158 if (!isStableLoopBound (otherBound, scope.bodyStart , scope.bodyEnd , stepTok, mSettings ))
51345159 continue ;
51355160
5136- // Address/reference may have escaped before the loop; a call in the body
5137- // might update the variable through that alias .
5138- if (addressOrRefTakenBefore (loopVarTok->variable (), forTok) && loopBodyHasCall (scope) )
5161+ // Address/reference escaped before the loop — the body (or an alias write
5162+ // like r++) may update the variable without naming it directly .
5163+ if (addressOrRefTakenBefore (loopVarTok->variable (), forTok))
51395164 continue ;
51405165
51415166 if (hasForLoopExit (scope, mSettings .library ))
0 commit comments