From 903df84ddc5a9ed91bd207d6e7b74759eab14815 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 13 Oct 2023 21:40:47 +0200 Subject: [PATCH] Fix #12072 FN constStatement with enum (#5554) --- lib/checkother.cpp | 4 +++- test/testincompletestatement.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 416885a5e..2a56127a5 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1892,7 +1892,7 @@ static bool isBracketAccess(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) @@ -2072,6 +2072,8 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type, typeStr = "character"; else if (isNullOperand(valueTok)) typeStr = "NULL"; + else if (valueTok->isEnumerator()) + typeStr = "enumerator"; msg = "Redundant code: Found a statement that begins with " + typeStr + " constant."; } else if (!tok) diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index e360c40b9..1cf105b0a 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -703,6 +703,13 @@ private: " auto g = [](decltype(a[0]) i) {};\n" "}\n"); 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() {