[clang-tools-extra] [clang-tidy] Improve readability-enum-initial-value diagnostic message (PR #176485)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 16 22:26:30 PST 2026
================
@@ -165,12 +166,29 @@ void EnumInitialValueCheck::registerMatchers(MatchFinder *Finder) {
void EnumInitialValueCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Enum = Result.Nodes.getNodeAs<EnumDecl>("inconsistent")) {
- const DiagnosticBuilder Diag =
- diag(
- Enum->getBeginLoc(),
- "initial values in enum '%0' are not consistent, consider explicit "
- "initialization of all, none or only the first enumerator")
- << getName(Enum);
+ llvm::SmallVector<StringRef, 4> UninitializedNames;
+ for (const EnumConstantDecl *ECD : Enum->enumerators())
+ if (ECD->getInitExpr() == nullptr && ECD->getDeclName())
+ UninitializedNames.push_back(ECD->getName());
+
+ llvm::SmallString<256> Message;
+ Message = "initial values in enum '%0' are not consistent, "
+ "consider explicit initialization of all, none or "
+ "only the first enumerator";
+ if (!UninitializedNames.empty()) {
+ Message += " (uninitialized enumerators: ";
+ for (size_t I = 0; I < UninitializedNames.size(); ++I) {
+ if (I > 0)
+ Message += (I < UninitializedNames.size() - 1) ? ", " : " and ";
+ Message += "'";
+ Message += UninitializedNames[I];
+ Message += "'";
----------------
vbvictor wrote:
Lets not add list of enumerators to main diagnostic. It becomes big and unreadable.
Add new note-diagnotics for each incorrect enum value.
So we would have:
```
warning: initial values in enum 'EError' are not consistent, consider explicit initialization of all, none or only the first enumerator
note: uninitialized enumerator 'EError_a' defined here
note: uninitialized enumerator 'EError_b' defined here
...
```
https://github.com/llvm/llvm-project/pull/176485
More information about the cfe-commits
mailing list