Skip to content

Commit 5c9be4a

Browse files
committed
Fixed #7216 (ValueFlow: known value in condition)
1 parent c0056d2 commit 5c9be4a

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,8 @@ static bool valueFlowForward(Token * const startToken,
11161116
}
11171117

11181118
// if known variable is changed in loop body, change it to a possible value..
1119-
handleKnownValuesInLoop(tok2, tok2->linkAt(1)->linkAt(1), &values, varid);
1119+
if (Token::Match(tok2, "for|while"))
1120+
handleKnownValuesInLoop(tok2, tok2->linkAt(1)->linkAt(1), &values, varid);
11201121

11211122
// Set values in condition
11221123
for (Token* tok3 = tok2->tokAt(2); tok3 != tok2->next()->link(); tok3 = tok3->next()) {

test/testvalueflow.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,14 @@ class TestValueFlow : public TestFixture {
17531753
ASSERT_EQUALS(3, value.intvalue);
17541754
ASSERT(value.isKnown());
17551755

1756+
code = "void f() {\n"
1757+
" int x = 15;\n"
1758+
" if (x == 15) { x += 7; }\n" // <- condition is true
1759+
"}";
1760+
value = valueOfTok(code, "==");
1761+
ASSERT_EQUALS(1, value.intvalue);
1762+
ASSERT(value.isKnown());
1763+
17561764
code = "void f() {\n"
17571765
" int x;\n"
17581766
" if (ab) { x = 7; }\n"

0 commit comments

Comments
 (0)