Fix #12072 FN constStatement with enum (#5554)

This commit is contained in:
chrchr-github 2023-10-13 21:40:47 +02:00 committed by GitHub
parent ec15772381
commit 903df84ddc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -1892,7 +1892,7 @@ static bool isBracketAccess(const Token* tok)
} }
static bool isConstant(const Token* tok) { static bool isConstant(const Token* tok) {
return Token::Match(tok, "%bool%|%num%|%str%|%char%|nullptr|NULL"); return tok && (tok->isEnumerator() || Token::Match(tok, "%bool%|%num%|%str%|%char%|nullptr|NULL"));
} }
static bool isConstStatement(const Token *tok, bool cpp) static bool isConstStatement(const Token *tok, bool cpp)
@ -2072,6 +2072,8 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type,
typeStr = "character"; typeStr = "character";
else if (isNullOperand(valueTok)) else if (isNullOperand(valueTok))
typeStr = "NULL"; typeStr = "NULL";
else if (valueTok->isEnumerator())
typeStr = "enumerator";
msg = "Redundant code: Found a statement that begins with " + typeStr + " constant."; msg = "Redundant code: Found a statement that begins with " + typeStr + " constant.";
} }
else if (!tok) else if (!tok)

View File

@ -703,6 +703,13 @@ private:
" auto g = [](decltype(a[0]) i) {};\n" " auto g = [](decltype(a[0]) i) {};\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("enum E { E0 };\n"
"void f() {\n"
" E0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (warning) Redundant code: Found a statement that begins with enumerator constant.\n",
errout.str());
} }
void vardecl() { void vardecl() {