1// RUN: %check_clang_tidy %s readability-enum-initial-value %t
2// RUN: %check_clang_tidy -check-suffix=ENABLE %s readability-enum-initial-value %t -- \
3// RUN: -config='{CheckOptions: { \
4// RUN: readability-enum-initial-value.AllowExplicitZeroFirstInitialValue: false, \
5// RUN: readability-enum-initial-value.AllowExplicitSequentialInitialValues: false, \
6// RUN: }}'
7
8enum EError {
9 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'EError' are not consistent
10 // CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum 'EError' are not consistent
11 EError_a = 1,
12 EError_b,
13 // CHECK-FIXES: EError_b = 2,
14 EError_c = 3,
15};
16
17enum ENone {
18 ENone_a,
19 ENone_b,
20 EENone_c,
21};
22
23enum EFirst {
24 EFirst_a = 1,
25 EFirst_b,
26 EFirst_c,
27};
28
29enum EAll {
30 EAll_a = 1,
31 EAll_b = 2,
32 EAll_c = 4,
33};
34
35#define ENUMERATOR_1 EMacro1_b
36enum EMacro1 {
37 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'EMacro1' are not consistent
38 // CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum 'EMacro1' are not consistent
39 EMacro1_a = 1,
40 ENUMERATOR_1,
41 // CHECK-FIXES: ENUMERATOR_1 = 2,
42 EMacro1_c = 3,
43};
44
45
46#define ENUMERATOR_2 EMacro2_b = 2
47enum EMacro2 {
48 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'EMacro2' are not consistent
49 // CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum 'EMacro2' are not consistent
50 EMacro2_a = 1,
51 ENUMERATOR_2,
52 EMacro2_c,
53 // CHECK-FIXES: EMacro2_c = 3,
54};
55
56
57enum {
58 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum '<unnamed>' are not consistent
59 // CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum '<unnamed>' are not consistent
60 EAnonymous_a = 1,
61 EAnonymous_b,
62 // CHECK-FIXES: EAnonymous_b = 2,
63 EAnonymous_c = 3,
64};
65
66
67enum EnumZeroFirstInitialValue {
68 EnumZeroFirstInitialValue_0 = 0,
69 // CHECK-MESSAGES-ENABLE: :[[@LINE-1]]:3: warning: zero initial value for the first enumerator in 'EnumZeroFirstInitialValue' can be disregarded
70 // CHECK-FIXES-ENABLE: EnumZeroFirstInitialValue_0 ,
71 EnumZeroFirstInitialValue_1,
72 EnumZeroFirstInitialValue_2,
73};
74
75enum EnumZeroFirstInitialValueWithComment {
76 EnumZeroFirstInitialValueWithComment_0 = /* == */ 0,
77 // CHECK-MESSAGES-ENABLE: :[[@LINE-1]]:3: warning: zero initial value for the first enumerator in 'EnumZeroFirstInitialValueWithComment' can be disregarded
78 // CHECK-FIXES-ENABLE: EnumZeroFirstInitialValueWithComment_0 /* == */ ,
79 EnumZeroFirstInitialValueWithComment_1,
80 EnumZeroFirstInitialValueWithComment_2,
81};
82
83enum EnumSequentialInitialValue {
84 // CHECK-MESSAGES-ENABLE: :[[@LINE-1]]:1: warning: sequential initial value in 'EnumSequentialInitialValue' can be ignored
85 EnumSequentialInitialValue_0 = 2,
86 // CHECK-FIXES-ENABLE: EnumSequentialInitialValue_0 = 2,
87 EnumSequentialInitialValue_1 = 3,
88 // CHECK-FIXES-ENABLE: EnumSequentialInitialValue_1 ,
89 EnumSequentialInitialValue_2 = 4,
90 // CHECK-FIXES-ENABLE: EnumSequentialInitialValue_2 ,
91};
92
93// gh107590
94enum WithFwdDeclInconsistent : int;
95
96enum WithFwdDeclInconsistent : int {
97 // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: initial values in enum 'WithFwdDeclInconsistent' are not consistent
98 // CHECK-MESSAGES-ENABLE: :[[@LINE-2]]:1: warning: initial values in enum 'WithFwdDeclInconsistent' are not consistent
99 EFI0,
100 // CHECK-FIXES: EFI0 = 0,
101 EFI1 = 1,
102 EFI2,
103 // CHECK-FIXES: EFI2 = 2,
104};
105
106enum WithFwdDeclZeroFirst : int;
107
108enum WithFwdDeclZeroFirst : int {
109 // CHECK-MESSAGES-ENABLE: :[[@LINE+1]]:3: warning: zero initial value for the first enumerator in 'WithFwdDeclZeroFirst' can be disregarded
110 EFZ0 = 0,
111 // CHECK-FIXES-ENABLE: EFZ0 ,
112 EFZ1,
113 EFZ2,
114};
115
116
117enum WithFwdDeclSequential : int;
118
119enum WithFwdDeclSequential : int {
120 // CHECK-MESSAGES-ENABLE: :[[@LINE-1]]:1: warning: sequential initial value in 'WithFwdDeclSequential' can be ignored
121 EFS0 = 2,
122 // CHECK-FIXES-ENABLE: EFS0 = 2,
123 EFS1 = 3,
124 // CHECK-FIXES-ENABLE: EFS1 ,
125 EFS2 = 4,
126 // CHECK-FIXES-ENABLE: EFS2 ,
127};
128

source code of clang-tools-extra/test/clang-tidy/checkers/readability/enum-initial-value.c