Skip to content

Commit 9df4447

Browse files
committed
Handle impossible values
1 parent bebcdcd commit 9df4447

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,10 +1145,20 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
11451145
value.setImpossible();
11461146
setTokenValue(tok, std::move(value), settings);
11471147
} else if (tok->isBinaryOp() && tok->str() == "|") {
1148-
const ValueFlow::Value* opVal = tok->astOperand1()->getKnownValue(ValueFlow::Value::ValueType::INT);
1149-
if (!opVal)
1150-
opVal = tok->astOperand2()->getKnownValue(ValueFlow::Value::ValueType::INT);
1151-
if (opVal && opVal->intvalue != 0) {
1148+
bool nonNull = false;
1149+
for (const Token *op : {tok->astOperand1(), tok->astOperand2()}) {
1150+
for (const ValueFlow::Value& v : op->values()) {
1151+
if (v.valueType != ValueFlow::Value::ValueType::INT)
1152+
continue;
1153+
if ((v.isKnown() && v.intvalue != 0) || (v.isImpossible() && v.intvalue == 0)) {
1154+
nonNull = true;
1155+
break;
1156+
}
1157+
}
1158+
if (nonNull)
1159+
break;
1160+
}
1161+
if (nonNull) {
11521162
ValueFlow::Value value{0};
11531163
value.setImpossible();
11541164
setTokenValue(tok, std::move(value), settings);

‎test/testcondition.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5003,8 +5003,15 @@ class TestCondition : public TestFixture {
50035003
check("void f(int i) {\n" // #15144
50045004
" int c = i | 1;\n"
50055005
" if (c) {}\n"
5006+
"}\n"
5007+
"void g(int i, int m) {\n"
5008+
" if (m == 0)\n"
5009+
" return;\n"
5010+
" int c = i | m;\n"
5011+
" if (c) {}\n"
50065012
"}\n");
5007-
ASSERT_EQUALS("[test.cpp:3:9]: (style) Condition 'c' is always true [knownConditionTrueFalse]\n",
5013+
ASSERT_EQUALS("[test.cpp:3:9]: (style) Condition 'c' is always true [knownConditionTrueFalse]\n"
5014+
"[test.cpp:9:9]: (style) Condition 'c' is always true [knownConditionTrueFalse]\n",
50085015
errout_str());
50095016
}
50105017

0 commit comments

Comments
 (0)