Fixed #10372 (Confusing message for compareValueOutOfTypeRangeError)

This commit is contained in:
Daniel Marjamäki 2021-08-10 09:38:28 +02:00
parent 0093452bed
commit d0b6079a83
2 changed files with 29 additions and 1 deletions

View File

@ -1807,7 +1807,9 @@ void CheckCondition::checkCompareValueOutOfTypeRange()
const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (-(1LL << (bits-1))) : 0;
const auto unsignedTypeMaxValue = (1LL << bits) - 1LL;
const auto typeMaxValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (unsignedTypeMaxValue / 2) : unsignedTypeMaxValue;
const auto typeMaxValue = (typeTok->valueType()->sign != ValueType::Sign::SIGNED || bits >= mSettings->int_bit) ?
unsignedTypeMaxValue : // unsigned type. signed int/long/long long; comparing sign bit is ok. i.e. 'i == 0xffffffff'
(unsignedTypeMaxValue / 2); // signed char/short
if (valueTok->getKnownIntValue() < typeMinValue)
compareValueOutOfTypeRangeError(valueTok, typeTok->valueType()->str(), valueTok->getKnownIntValue());

View File

@ -4439,6 +4439,32 @@ private:
" if (b == true) {}\n"
"}", &settingsUnix64);
ASSERT_EQUALS("", errout.str());
// #10372
check("void f(signed char x) {\n"
" if (x == 0xff) {}\n"
"}", &settingsUnix64);
ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'signed char' against value 255. Condition is always true/false.\n", errout.str());
check("void f(short x) {\n"
" if (x == 0xffff) {}\n"
"}", &settingsUnix64);
ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'signed short' against value 65535. Condition is always true/false.\n", errout.str());
check("void f(int x) {\n"
" if (x == 0xffffffff) {}\n"
"}", &settingsUnix64);
ASSERT_EQUALS("", errout.str());
check("void f(long x) {\n"
" if (x == ~0L) {}\n"
"}", &settingsUnix64);
ASSERT_EQUALS("", errout.str());
check("void f(long long x) {\n"
" if (x == ~0LL) {}\n"
"}", &settingsUnix64);
ASSERT_EQUALS("", errout.str());
}
void knownConditionCast() { // #9976