Skip to content

Commit a5a7dd7

Browse files
Fix cppcheck-opensource#7959 invalidPrintfArgType_uint false positive (cppcheck-opensource#8534)
1 parent 2807d6e commit a5a7dd7

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

lib/checkio.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,9 +1539,10 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * arg, const Settings &settings,
15391539
if (element && isStdVectorOrString()) { // isStdVectorOrString sets type token if true
15401540
element = false; // not really an array element
15411541
} else if (variableInfo->isEnumType()) {
1542-
if (variableInfo->type() && variableInfo->type()->classScope && variableInfo->type()->classScope->enumType)
1542+
const bool hasEnumType = variableInfo->type() && variableInfo->type()->classScope && variableInfo->type()->classScope->enumType;
1543+
if (hasEnumType && variableInfo->type()->classScope->enumType->isStandardType())
15431544
typeToken = variableInfo->type()->classScope->enumType;
1544-
else {
1545+
else if (!hasEnumType) {
15451546
tempToken = new Token(tok1);
15461547
tempToken->str("int");
15471548
typeToken = tempToken;

test/testio.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3371,6 +3371,20 @@ class TestIO : public TestFixture {
33713371
"}");
33723372
ASSERT_EQUALS("", errout_str());
33733373

3374+
check("enum E : uint8_t { E0 }; \n" // #7959
3375+
"void f(E e) {\n"
3376+
" printf(\"%hhu\", e);\n"
3377+
"}");
3378+
ASSERT_EQUALS("", errout_str());
3379+
3380+
check("enum E : uint8_t { E0 }; \n"
3381+
"void f(E e) {\n"
3382+
" printf(\"%lu\", e);\n"
3383+
"}");
3384+
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) %lu in format string (no. 1) requires 'unsigned long' but the argument type is 'uint8_t'.\n",
3385+
"",
3386+
errout_str());
3387+
33743388
check("void f() {\n"
33753389
" printf(\"%lu\", sizeof(char));\n"
33763390
"}\n", dinit(CheckOptions, $.portability = true, $.platform = Platform::Type::Win64));

0 commit comments

Comments
 (0)