Skip to content

Commit a391bca

Browse files
committed
Fixed #7350 (ValueFlow: Result of 'x & 0' is always 0)
1 parent 5e10e68 commit a391bca

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value)
398398
parent->astOperand1()->values.front().isKnown()) ||
399399
(parent->astOperand2()->values.size() == 1U &&
400400
parent->astOperand2()->values.front().isKnown()));
401+
402+
// known result when a operand is 0.
403+
if (Token::Match(parent, "[&*]") && value.isKnown() && value.tokvalue==nullptr && value.intvalue==0) {
404+
setTokenValue(parent, value);
405+
return;
406+
}
407+
401408
std::list<ValueFlow::Value>::const_iterator value1, value2;
402409
for (value1 = parent->astOperand1()->values.begin(); value1 != parent->astOperand1()->values.end(); ++value1) {
403410
if (value1->tokvalue && (!parent->isComparisonOp() || value1->tokvalue->tokType() != Token::eString))

test/testvalueflow.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,12 @@ class TestValueFlow : public TestFixture {
18831883
"}";
18841884
ASSERT_EQUALS(true, testValueOfX(code, 3U, 1)); // value of x can be 1
18851885
ASSERT_EQUALS(false, testValueOfX(code, 3U, 2)); // value of x can't be 2
1886+
1887+
// calculation with known result
1888+
code = "int f(int x) { a = x & 0; }"; // <- & is 0
1889+
value = valueOfTok(code, "&");
1890+
ASSERT_EQUALS(0, value.intvalue);
1891+
ASSERT(value.isKnown());
18861892
}
18871893
};
18881894

0 commit comments

Comments
 (0)