[clang-tools-extra] [clang-tidy] add new check readability-enum-initial-value (PR #86129)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 26 10:52:54 PDT 2024


================
@@ -0,0 +1,75 @@
+.. title:: clang-tidy - readability-enum-initial-value
+
+readability-enum-initial-value
+==============================
+
+Detects explicit initialization of a part of enumerators in an enumeration, and
+relying on compiler to initialize the others.
+
+When adding new enumerations, inconsistent initial value will cause potential
+enumeration value conflicts.
+
+In an enumeration, the following three cases are accepted. 
+1. none of enumerators are explicit initialized.
+2. the first enumerator is explicit initialized.
+3. all of enumerators are explicit initialized.
+
+.. code-block:: c++
+
+  // valid, none of enumerators are initialized.
+  enum A {
+    e0,
+    e1,
+    e2,
+  };
+
+  // valid, the first enumerator is initialized.
+  enum A {
+    e0 = 0,
+    e1,
+    e2,
+  };
+
+  // valid, all of enumerators are initialized.
+  enum A {
+    e0 = 0,
+    e1 = 1,
+    e2 = 2,
+  };
+
+  // invalid, e1 is not explicit initialized.
+  enum A {
+    e0 = 0,
+    e1,
+    e2 = 2,
+  };
+
+Options
+-------
+
+.. option:: AllowExplicitZeroFirstInitialValue
+
+  If set to `false`, explicit initialized first enumerator is not allowed.
----------------
PiotrZSL wrote:

```suggestion
  If set to `false`, the first enumerator must not be explicitly initialized.
```

https://github.com/llvm/llvm-project/pull/86129


More information about the cfe-commits mailing list