Skip to content

Commit

Permalink
Fix #12072 FN constStatement with enum (danmar#5554)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Oct 13, 2023
1 parent ec15772 commit 903df84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/testincompletestatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 903df84

Please sign in to comment.