Fix 11203: false positive: knownConditionTrueFalse 'always false' when comparing integer with floating-point (#4350)

This commit is contained in:
Paul Fultz II 2022-09-08 12:59:02 -05:00 committed by GitHub
parent 48874e3aaf
commit 7c986fbef1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -836,10 +836,12 @@ static void setTokenValue(Token* tok,
}
const double floatValue1 = value1.isFloatValue() ? value1.floatValue : value1.intvalue;
const double floatValue2 = value2.isFloatValue() ? value2.floatValue : value2.intvalue;
const MathLib::bigint intValue1 =
value1.isFloatValue() ? std::llround(value1.floatValue) : value1.intvalue;
const MathLib::bigint intValue2 =
value2.isFloatValue() ? std::llround(value2.floatValue) : value2.intvalue;
const auto intValue1 = [&]() -> MathLib::bigint {
return value1.isFloatValue() ? static_cast<MathLib::bigint>(value1.floatValue) : value1.intvalue;
};
const auto intValue2 = [&]() -> MathLib::bigint {
return value2.isFloatValue() ? static_cast<MathLib::bigint>(value2.floatValue) : value2.intvalue;
};
if ((value1.isFloatValue() || value2.isFloatValue()) && Token::Match(parent, "&|^|%|<<|>>|==|!=|%or%"))
continue;
if (Token::Match(parent, "==|!=")) {
@ -850,7 +852,7 @@ static void setTokenValue(Token* tok,
result.intvalue = 1;
} else if (value1.isIntValue() && value2.isIntValue()) {
bool error = false;
result.intvalue = calculate(parent->str(), intValue1, intValue2, &error);
result.intvalue = calculate(parent->str(), intValue1(), intValue2(), &error);
if (error)
continue;
} else {
@ -869,7 +871,7 @@ static void setTokenValue(Token* tok,
if (result.isFloatValue()) {
result.floatValue = calculate(parent->str(), floatValue1, floatValue2, &error);
} else {
result.intvalue = calculate(parent->str(), intValue1, intValue2, &error);
result.intvalue = calculate(parent->str(), intValue1(), intValue2(), &error);
}
if (error)
continue;

View File

@ -4283,6 +4283,16 @@ private:
"};\n");
ASSERT_EQUALS("", errout.str());
// #11203
check("void f() {\n"
" int i = 10;\n"
" if(i > 9.9){}\n"
" float f = 9.9f;\n"
" if(f < 10) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Condition 'i>9.9' is always true\n"
"[test.cpp:5]: (style) Condition 'f<10' is always true\n",
errout.str());
check("constexpr int f() {\n" // #11238
" return 1;\n"
"}\n"