diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 416885a5e14..2a56127a5f7 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 e360c40b9a9..1cf105b0ace 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -703,6 +703,13 @@ class TestIncompleteStatement : public TestFixture { " 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() {