Skip to content

Commit bc6d31c

Browse files
committed
unsigned division: don't warn about 'unsigned char' because it is promoted to int.
1 parent 509dd9a commit bc6d31c

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

lib/checkother.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,11 +1808,11 @@ void CheckOther::unreachableCodeError(const Token *tok, bool inconclusive)
18081808
//---------------------------------------------------------------------------
18091809
// Check for unsigned divisions
18101810
//---------------------------------------------------------------------------
1811-
static bool isUnsigned(const Variable* var)
1811+
bool CheckOther::isUnsigned(const Variable* var) const
18121812
{
1813-
return(var && var->typeStartToken()->isUnsigned() && !var->isPointer() && !var->isArray());
1813+
return(var && var->typeStartToken()->isUnsigned() && !var->isPointer() && !var->isArray() && _tokenizer->sizeOfType(var->typeStartToken()) >= _settings->sizeof_int);
18141814
}
1815-
static bool isSigned(const Variable* var)
1815+
bool CheckOther::isSigned(const Variable* var) const
18161816
{
18171817
return(var && !var->typeStartToken()->isUnsigned() && Token::Match(var->typeEndToken(), "int|char|short|long") && !var->isPointer() && !var->isArray());
18181818
}

lib/checkother.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ class CPPCHECKLIB CheckOther : public Check {
294294
void checkVarFuncNullUB();
295295

296296
private:
297+
bool isUnsigned(const Variable *var) const;
298+
bool isSigned(const Variable *var) const;
299+
297300
// Error messages..
298301
void oppositeInnerConditionError(const Token *tok);
299302
void clarifyCalculationError(const Token *tok, const std::string &op);

test/testdivision.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class TestDivision : public TestFixture {
6363
TEST_CASE(division8);
6464
TEST_CASE(division9);
6565
TEST_CASE(division10);
66+
TEST_CASE(division11); // no error when using "unsigned char" (it is promoted)
6667
}
6768

6869
void division1() {
@@ -197,6 +198,13 @@ class TestDivision : public TestFixture {
197198
check("i / i", true);
198199
ASSERT_EQUALS("", errout.str());
199200
}
201+
202+
void division11() {
203+
check("void f(int x, unsigned char y) {\n"
204+
" int z = x / y;\n" // no error, y is promoted
205+
"}", true);
206+
ASSERT_EQUALS("", errout.str());
207+
}
200208
};
201209

202210
REGISTER_TEST(TestDivision)

0 commit comments

Comments
 (0)