Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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<int*>& 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());
}
};

Expand Down