Skip to content

Commit 553b579

Browse files
Fix #11434 FP knownConditionTrueFalse with loop over bool array (#4646)
* Fix #11434 FP knownConditionTrueFalse with loop over bool array * Simplify
1 parent 0c1e2ce commit 553b579

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

lib/astutils.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,10 +1370,23 @@ static bool isForLoopCondition(const Token * const tok)
13701370
parent->astParent()->astParent()->astOperand1()->str() == "for";
13711371
}
13721372

1373+
static bool isForLoopIncrement(const Token* const tok)
1374+
{
1375+
if (!tok)
1376+
return false;
1377+
const Token *const parent = tok->astParent();
1378+
return Token::simpleMatch(parent, ";") && parent->astOperand2() == tok &&
1379+
Token::simpleMatch(parent->astParent(), ";") &&
1380+
Token::simpleMatch(parent->astParent()->astParent(), "(") &&
1381+
parent->astParent()->astParent()->astOperand1()->str() == "for";
1382+
}
1383+
13731384
bool isUsedAsBool(const Token* const tok)
13741385
{
13751386
if (!tok)
13761387
return false;
1388+
if (isForLoopIncrement(tok))
1389+
return false;
13771390
if (astIsBool(tok))
13781391
return true;
13791392
if (Token::Match(tok, "!|&&|%oror%|%comp%"))

test/testcondition.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4973,6 +4973,13 @@ class TestCondition : public TestFixture {
49734973
" }\n"
49744974
"}\n");
49754975
ASSERT_EQUALS("", errout.str());
4976+
4977+
check("void f() {\n" // #11434
4978+
" const int N = 5;\n"
4979+
" bool a[N];\n"
4980+
" for (int i = 0; i < N; a[i++] = false);\n"
4981+
"}\n");
4982+
ASSERT_EQUALS("", errout.str());
49764983
}
49774984

49784985
void alwaysTrueTryCatch()

0 commit comments

Comments
 (0)