unsigned division: don't warn about 'unsigned char' because it is promoted to int.
This commit is contained in:
parent
509dd9a989
commit
bc6d31c7c7
|
@ -1808,11 +1808,11 @@ void CheckOther::unreachableCodeError(const Token *tok, bool inconclusive)
|
|||
//---------------------------------------------------------------------------
|
||||
// Check for unsigned divisions
|
||||
//---------------------------------------------------------------------------
|
||||
static bool isUnsigned(const Variable* var)
|
||||
bool CheckOther::isUnsigned(const Variable* var) const
|
||||
{
|
||||
return(var && var->typeStartToken()->isUnsigned() && !var->isPointer() && !var->isArray());
|
||||
return(var && var->typeStartToken()->isUnsigned() && !var->isPointer() && !var->isArray() && _tokenizer->sizeOfType(var->typeStartToken()) >= _settings->sizeof_int);
|
||||
}
|
||||
static bool isSigned(const Variable* var)
|
||||
bool CheckOther::isSigned(const Variable* var) const
|
||||
{
|
||||
return(var && !var->typeStartToken()->isUnsigned() && Token::Match(var->typeEndToken(), "int|char|short|long") && !var->isPointer() && !var->isArray());
|
||||
}
|
||||
|
|
|
@ -294,6 +294,9 @@ public:
|
|||
void checkVarFuncNullUB();
|
||||
|
||||
private:
|
||||
bool isUnsigned(const Variable *var) const;
|
||||
bool isSigned(const Variable *var) const;
|
||||
|
||||
// Error messages..
|
||||
void oppositeInnerConditionError(const Token *tok);
|
||||
void clarifyCalculationError(const Token *tok, const std::string &op);
|
||||
|
|
|
@ -63,6 +63,7 @@ private:
|
|||
TEST_CASE(division8);
|
||||
TEST_CASE(division9);
|
||||
TEST_CASE(division10);
|
||||
TEST_CASE(division11); // no error when using "unsigned char" (it is promoted)
|
||||
}
|
||||
|
||||
void division1() {
|
||||
|
@ -197,6 +198,13 @@ private:
|
|||
check("i / i", true);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void division11() {
|
||||
check("void f(int x, unsigned char y) {\n"
|
||||
" int z = x / y;\n" // no error, y is promoted
|
||||
"}", true);
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
};
|
||||
|
||||
REGISTER_TEST(TestDivision)
|
||||
|
|
Loading…
Reference in New Issue