diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 1492714c1c1..e18f9f4c69f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -4914,13 +4914,12 @@ static bool stepUpdatesVar(const Token* stepTok, nonneg int varId) { if (!stepTok || varId == 0) return false; - if (stepTok->str() == ",") - return stepUpdatesVar(stepTok->astOperand1(), varId) || stepUpdatesVar(stepTok->astOperand2(), varId); if (stepTok->isIncDecOp() && stepTok->astOperand1() && stepTok->astOperand1()->varId() == varId) return true; - if (stepTok->isAssignmentOp() && stepTok->astOperand1() && stepTok->astOperand1()->varId() == varId) // TODO: WILL THIS RETURN TRUE IF STEP IS SOMETHING LIKE: i=i + if (stepTok->isAssignmentOp() && stepTok->astOperand1() && stepTok->astOperand1()->varId() == varId) return true; - return false; + // Recurse so nested updates are found, e.g. "delete *it++" or "a = b++" + return stepUpdatesVar(stepTok->astOperand1(), varId) || stepUpdatesVar(stepTok->astOperand2(), varId); } static bool hasForLoopExit(const Scope& scope) @@ -4938,7 +4937,7 @@ static bool hasForLoopExit(const Scope& scope) } if (!tok->isName() || !tok->isKeyword()) continue; - if (Token::Match(tok, "goto|return|throw")) //TODO: WILL THIS RETURN TRUE IF GOTO STAYS WITHIN THE LOOP + if (Token::Match(tok, "goto|return|throw")) return true; if (tok->str() != "break") continue; diff --git a/test/testother.cpp b/test/testother.cpp index e1b9e2be493..37ff9aa7b4d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6682,7 +6682,8 @@ class TestOther : public TestFixture { " c ++;\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2:31]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:31]: (warning, inconclusive) Found suspicious equality comparison. Did you intend to assign a value instead? [constStatement]\n" + "[test.cpp:2:5]: (warning) The loop variable 'i' is not updated in this for-loop, so the loop might never terminate. [missingForLoopIncrement]\n", errout_str()); check("void foo(int c) {\n" " for (; running == 1;) {\n" @@ -8641,7 +8642,8 @@ class TestOther : public TestFixture { " if( i != 0 ) {}\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:2:17] -> [test.cpp:3:15]: (style) The comparison 'i != 0' is always false. [knownConditionTrueFalse]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:17] -> [test.cpp:3:15]: (style) The comparison 'i != 0' is always false. [knownConditionTrueFalse]\n" + "[test.cpp:2:5]: (warning) The loop variable 'i' is not updated in this for-loop, so the loop might never terminate. [missingForLoopIncrement]\n", errout_str()); check("void f() {\n" " for(int i = 0; i < 10;) {\n" @@ -14694,6 +14696,21 @@ class TestOther : public TestFixture { " }\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + // Nested update in step — OK + check("void f(const std::vector& v) {\n" + " for (auto it = v.begin(); it != v.end(); delete *it++);\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + // Loop var updated on RHS of step assignment — OK + check("void dostuff(int);\n" + "void foo(int num) {\n" + " int a = num;\n" + " for (int b = 0; b < num; a = b++)\n" + " dostuff(a);\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } };