@@ -1176,6 +1176,14 @@ static int estimateSize(const Type* type, const Settings* settings, const Symbol
11761176 });
11771177}
11781178
1179+ static bool isConstRangeBasedFor (const Token* tok) {
1180+ if (astIsRangeBasedForDecl (tok)) {
1181+ const Variable* loopVar = tok->astParent ()->astOperand1 ()->variable ();
1182+ return loopVar && (!loopVar->isReference () || loopVar->isConst ());
1183+ }
1184+ return false ;
1185+ }
1186+
11791187static bool canBeConst (const Variable *var, const Settings* settings)
11801188{
11811189 if (!var->scope ())
@@ -1201,6 +1209,8 @@ static bool canBeConst(const Variable *var, const Settings* settings)
12011209 continue ;
12021210
12031211 const Token* parent = tok2->astParent ();
1212+ while (Token::simpleMatch (parent, " [" ))
1213+ parent = parent->astParent ();
12041214 if (!parent)
12051215 continue ;
12061216 if (Token::simpleMatch (tok2->next (), " ;" ) && tok2->next ()->isSplittedVarDeclEq ()) {
@@ -1249,9 +1259,12 @@ static bool canBeConst(const Variable *var, const Settings* settings)
12491259 (parent->astOperand2 () && settings->library .isFunctionConst (parent->astOperand2 ())))
12501260 continue ;
12511261 else if (parent->isAssignmentOp ()) {
1252- if (parent->astOperand1 () == tok2)
1262+ const Token* assignee = parent->astOperand1 ();
1263+ while (Token::simpleMatch (assignee, " [" ))
1264+ assignee = assignee->astOperand1 ();
1265+ if (assignee == tok2)
12531266 return false ;
1254- const Variable* assignedVar = parent-> astOperand1 () ? parent-> astOperand1 () ->variable () : nullptr ;
1267+ const Variable* assignedVar = assignee ? assignee ->variable () : nullptr ;
12551268 if (assignedVar &&
12561269 !assignedVar->isConst () &&
12571270 assignedVar->isReference () &&
@@ -1263,7 +1276,9 @@ static bool canBeConst(const Variable *var, const Settings* settings)
12631276 continue ;
12641277 else
12651278 return false ;
1266- } else
1279+ } else if (isConstRangeBasedFor (tok2))
1280+ continue ;
1281+ else
12671282 return false ;
12681283 }
12691284
0 commit comments