@@ -820,9 +820,34 @@ static bool valueFlowForward(Token * const startToken,
820820 ++number_of_if;
821821 tok2 = end;
822822 } else {
823- if (settings->debugwarnings )
824- bailout (tokenlist, errorLogger, tok2, " variable " + var->name () + " is assigned in conditional code" );
825- return false ;
823+ bool bail = true ;
824+
825+ // loop that conditionally set variable and then break => either loop condition is
826+ // redundant or the variable can be unchanged after the loop.
827+ bool loopCondition = false ;
828+ if (Token::simpleMatch (tok2, " while (" ) && Token::Match (tok2->next ()->astOperand2 (), " %op%" ))
829+ loopCondition = true ;
830+ else if (Token::simpleMatch (tok2, " for (" ) &&
831+ Token::simpleMatch (tok2->next ()->astOperand2 (), " ;" ) &&
832+ Token::simpleMatch (tok2->next ()->astOperand2 ()->astOperand2 (), " ;" ) &&
833+ Token::Match (tok2->next ()->astOperand2 ()->astOperand2 ()->astOperand1 (), " %op%" ))
834+ loopCondition = true ;
835+ if (loopCondition) {
836+ const Token *tok3 = Token::findmatch (start, " %varid%" , end, varid);
837+ if (Token::Match (tok3, " %varid% =" , varid) &&
838+ tok3->scope ()->classEnd &&
839+ Token::Match (tok3->scope ()->classEnd ->tokAt (-3 ), " [;}] break ;" ) &&
840+ !Token::findmatch (tok3->next (), " %varid%" , end, varid)) {
841+ bail = false ;
842+ tok2 = end;
843+ }
844+ }
845+
846+ if (bail) {
847+ if (settings->debugwarnings )
848+ bailout (tokenlist, errorLogger, tok2, " variable " + var->name () + " is assigned in conditional code" );
849+ return false ;
850+ }
826851 }
827852 }
828853 }
0 commit comments