diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c6f11f1ba..6625c33d8 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -273,7 +273,7 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog continue; } - // bailout: while-condition, variable is changed in while loop + // bailout: for/while-condition, variable is changed in while loop for (const Token *tok2 = tok; tok2; tok2 = tok2->astParent()) { if (tok2->astParent() || tok2->str() != "(" || !Token::simpleMatch(tok2->link(), ") {")) continue; @@ -281,7 +281,14 @@ static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLog if (Token::Match(tok2->previous(), "for|while (")) { const Token *start = tok2->link()->next(); const Token *end = start->link(); - if (Token::findmatch(start,"++|--| %varid% ++|--|=",end,varid)) { + + if (tok2->astOperand2()->str() == ";" && + tok2->astOperand2()->astOperand2() && + tok2->astOperand2()->astOperand2()->str() == ";") + start = tok2->astOperand2()->astOperand2(); + + if (Token::findmatch(start,"++|-- %varid%",end,varid) || + Token::findmatch(start,"%varid% ++|--|=",end,varid)) { varid = 0U; if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok, "variable " + var->nameToken()->str() + " used in loop"); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 03e268373..fd3ca43ff 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -333,6 +333,13 @@ private: " }\n" "}"; ASSERT_EQUALS(false, testValueOfX(code, 2U, 0)); + + code = "void f(unsigned int x) {\n" + " for (int a = 1000 / x;\n" + " x > 0;\n" + " x--) {}\n" + "}"; + ASSERT_EQUALS(false, testValueOfX(code, 2U, 0)); } void valueFlowBeforeConditionTernaryOp() { // bailout: ?: