Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/readability/enum-initial-value.rst

raw
1 .. title:: clang-tidy - readability-enum-initial-value
2
3 readability-enum-initial-value
4 ==============================
5
6 Enforces consistent style for enumerators' initialization, covering three
7 styles: none, first only, or all initialized explicitly.
8
9 An inconsistent style and strictness to defining the initializing value of
10 enumerators may cause issues if the enumeration is extended with new
11 enumerators that obtain their integer representation implicitly.
12
13 The following three cases are accepted:
14
15 #. **No** enumerators are explicit initialized.
16 #. Exactly **the first** enumerator is explicit initialized.
17 #. **All** enumerators are explicit initialized.
18
19 .. code-block:: c++
20
21 enum A { // (1) Valid, none of enumerators are initialized.
22 a0,
23 a1,
24 a2,
25 };
26
27 enum B { // (2) Valid, the first enumerator is initialized.
28 b0 = 0,
29 b1,
30 b2,
31 };
32
33 enum C { // (3) Valid, all of enumerators are initialized.
34 c0 = 0,
35 c1 = 1,
36 c2 = 2,
37 };
38
39 enum D { // Invalid, d1 is not explicitly initialized!
40 d0 = 0,
41 d1,
42 d2 = 2,
43 };
44
45 enum E { // Invalid, e1, e3, and e5 are not explicitly initialized.
46 e0 = 0,
47 e1,
48 e2 = 2,
49 e3, // Dangerous, as the numeric values of e3 and e5 are both 3, and this is not explicitly visible in the code!
50 e4 = 2,
51 e5,
52 };
53
54 This check corresponds to the CERT C Coding Standard recommendation `INT09-C. Ensure enumeration constants map to unique values
55 <https://wiki.sei.cmu.edu/confluence/display/c/INT09-C.+Ensure+enumeration+constants+map+to+unique+values>`_.
56
57 `cert-int09-c` redirects here as an alias of this check.
58
59 Options
60 -------
61
62 .. option:: AllowExplicitZeroFirstInitialValue
63
64 If set to `false`, the first enumerator must not be explicitly initialized to
65 a literal ``0``.
66 Default is `true`.
67
68 .. code-block:: c++
69
70 enum F {
71 f0 = 0, // Not allowed if AllowExplicitZeroFirstInitialValue is false.
72 f1,
73 f2,
74 };
75
76
77 .. option:: AllowExplicitSequentialInitialValues
78
79 If set to `false`, explicit initialization to sequential values are not
80 allowed.
81 Default is `true`.
82
83 .. code-block:: c++
84
85 enum G {
86 g0 = 1, // Not allowed if AllowExplicitSequentialInitialValues is false.
87 g1 = 2,
88 g2 = 3,