You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[clang-tidy] New Option Invalid Enum Default Initialization (#159220)
Added a new Option IgnoredEnums to bugprone invalid enum default
initialization to limit the scope of the analysis. This is needed to
remove warnings on enums like std::errc where the enum doesn't define a
value of 0, but is still used to check if some function calls like
std::from_chars are executed correctly.
The C++ Standard section 22.13.2 mentions the following : "[...] If the
member ec of the return value is such that the value is equal to the
value of a value-initialized errc, the conversion was successful [...]"
This means that a call to `std::errc{}` is clearly defined by the
standard and should not raise any warning under this check.
// CHECK-NOTES: :[[@LINE-1]]:11: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
27
-
// CHECK-NOTES: :8:12: note: enum is defined here
28
+
// CHECK-NOTES: :9:12: note: enum is defined here
28
29
Enum1 E1_2 = Enum1();
29
30
// CHECK-NOTES: :[[@LINE-1]]:14: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
30
-
// CHECK-NOTES: :8:12: note: enum is defined here
31
+
// CHECK-NOTES: :9:12: note: enum is defined here
31
32
Enum1 E1_3;
32
33
Enum1 E1_4{0};
33
34
Enum1 E1_5{Enum1::A};
34
35
Enum1 E1_6{Enum1::B};
35
36
36
37
Enum2 E2_1{};
37
38
// CHECK-NOTES: :[[@LINE-1]]:11: warning: enum value of type 'Enum2' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
38
-
// CHECK-NOTES: :13:6: note: enum is defined here
39
+
// CHECK-NOTES: :14:6: note: enum is defined here
39
40
Enum2 E2_2 = Enum2();
40
41
// CHECK-NOTES: :[[@LINE-1]]:14: warning: enum value of type 'Enum2' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
41
-
// CHECK-NOTES: :13:6: note: enum is defined here
42
+
// CHECK-NOTES: :14:6: note: enum is defined here
42
43
43
44
voidf1() {
44
45
static Enum1 S; // FIMXE: warn for this?
45
46
Enum1 A;
46
47
Enum1 B = Enum1();
47
48
// CHECK-NOTES: :[[@LINE-1]]:13: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
48
-
// CHECK-NOTES: :8:12: note: enum is defined here
49
+
// CHECK-NOTES: :9:12: note: enum is defined here
49
50
int C = int();
50
51
}
51
52
52
53
voidf2() {
53
54
Enum1 A{};
54
55
// CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
55
-
// CHECK-NOTES: :8:12: note: enum is defined here
56
+
// CHECK-NOTES: :9:12: note: enum is defined here
56
57
Enum1 B = Enum1();
57
58
// CHECK-NOTES: :[[@LINE-1]]:13: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
58
-
// CHECK-NOTES: :8:12: note: enum is defined here
59
+
// CHECK-NOTES: :9:12: note: enum is defined here
59
60
Enum1 C[5] = {{}};
60
61
// CHECK-NOTES: :[[@LINE-1]]:16: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
61
-
// CHECK-NOTES: :8:12: note: enum is defined here
62
+
// CHECK-NOTES: :9:12: note: enum is defined here
62
63
// CHECK-NOTES: :[[@LINE-3]]:17: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
63
-
// CHECK-NOTES: :8:12: note: enum is defined here
64
+
// CHECK-NOTES: :9:12: note: enum is defined here
64
65
Enum1 D[5] = {}; // FIMXE: warn for this?
65
66
// CHECK-NOTES: :[[@LINE-1]]:16: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
66
-
// CHECK-NOTES: :8:12: note: enum is defined here
67
+
// CHECK-NOTES: :9:12: note: enum is defined here
67
68
}
68
69
69
70
structS1 {
70
71
Enum1 E_1{};
71
72
// CHECK-NOTES: :[[@LINE-1]]:12: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
72
-
// CHECK-NOTES: :8:12: note: enum is defined here
73
+
// CHECK-NOTES: :9:12: note: enum is defined here
73
74
Enum1 E_2 = Enum1();
74
75
// CHECK-NOTES: :[[@LINE-1]]:15: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
75
-
// CHECK-NOTES: :8:12: note: enum is defined here
76
+
// CHECK-NOTES: :9:12: note: enum is defined here
76
77
Enum1 E_3;
77
78
Enum1 E_4;
78
79
Enum1 E_5;
79
80
80
81
S1() :
81
82
E_3{},
82
83
// CHECK-NOTES: :[[@LINE-1]]:8: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
83
-
// CHECK-NOTES: :8:12: note: enum is defined here
84
+
// CHECK-NOTES: :9:12: note: enum is defined here
84
85
E_4(),
85
86
// CHECK-NOTES: :[[@LINE-1]]:8: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
86
-
// CHECK-NOTES: :8:12: note: enum is defined here
87
+
// CHECK-NOTES: :9:12: note: enum is defined here
87
88
E_5{Enum1::B}
88
89
{}
89
90
};
@@ -110,22 +111,22 @@ struct S5 {
110
111
111
112
S2 VarS2{};
112
113
// CHECK-NOTES: :[[@LINE-1]]:9: warning: enum value of type 'Enum1' initialized with invalid value of 0
113
-
// CHECK-NOTES: :8:12: note: enum is defined here
114
+
// CHECK-NOTES: :9:12: note: enum is defined here
114
115
// CHECK-NOTES: :[[@LINE-3]]:9: warning: enum value of type 'Enum2' initialized with invalid value of 0
115
-
// CHECK-NOTES: :13:6: note: enum is defined here
116
+
// CHECK-NOTES: :14:6: note: enum is defined here
116
117
S3 VarS3{};
117
118
// CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0
118
-
// CHECK-NOTES: :8:12: note: enum is defined here
119
+
// CHECK-NOTES: :9:12: note: enum is defined here
119
120
// CHECK-NOTES: :[[@LINE-3]]:10: warning: enum value of type 'Enum2' initialized with invalid value of 0
120
-
// CHECK-NOTES: :13:6: note: enum is defined here
121
+
// CHECK-NOTES: :14:6: note: enum is defined here
121
122
S4 VarS4{};
122
123
// CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0
123
-
// CHECK-NOTES: :8:12: note: enum is defined here
124
+
// CHECK-NOTES: :9:12: note: enum is defined here
124
125
// CHECK-NOTES: :[[@LINE-3]]:10: warning: enum value of type 'Enum2' initialized with invalid value of 0
125
-
// CHECK-NOTES: :13:6: note: enum is defined here
126
+
// CHECK-NOTES: :14:6: note: enum is defined here
126
127
S5 VarS5{};
127
128
// CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0
128
-
// CHECK-NOTES: :8:12: note: enum is defined here
129
+
// CHECK-NOTES: :9:12: note: enum is defined here
129
130
130
131
enumclassEnumFwd;
131
132
@@ -139,7 +140,25 @@ template<typename T>
139
140
structTempl {
140
141
T Mem1{};
141
142
// CHECK-NOTES: :[[@LINE-1]]:9: warning: enum value of type 'Enum1' initialized with invalid value of 0
142
-
// CHECK-NOTES: :8:12: note: enum is defined here
143
+
// CHECK-NOTES: :9:12: note: enum is defined here
143
144
};
144
145
145
146
Templ<Enum1> TemplVar;
147
+
148
+
enum MyEnum {
149
+
A = 1,
150
+
B
151
+
};
152
+
153
+
MyEnum MyEnumVar{};
154
+
// CHECK-NOTES-DEFAULT: :[[@LINE-1]]:17: warning: enum value of type 'MyEnum' initialized with invalid value of 0, enum doesn't have a zero-value enumerator
155
+
// CHECK-NOTES-DEFAULT: :148:6: note: enum is defined here
0 commit comments