165 lines · cpp
1// RUN: %check_clang_tidy -check-suffixes=,DEFAULT -std=c++17-or-later %s bugprone-invalid-enum-default-initialization %t2// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-invalid-enum-default-initialization %t -- -config="{CheckOptions: {bugprone-invalid-enum-default-initialization.IgnoredEnums: '::MyEnum'}}"3 4enum class Enum0: int {5 A = 0,6 B7};8 9enum class Enum1: int {10 A = 1,11 B12};13 14enum Enum2 {15 Enum_A = 4,16 Enum_B17};18 19Enum0 E0_1{};20Enum0 E0_2 = Enum0();21Enum0 E0_3;22Enum0 E0_4{0};23Enum0 E0_5{Enum0::A};24Enum0 E0_6{Enum0::B};25 26Enum1 E1_1{};27// CHECK-NOTES: :[[@LINE-1]]:11: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator28// CHECK-NOTES: :9:12: note: enum is defined here29Enum1 E1_2 = Enum1();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 enumerator31// CHECK-NOTES: :9:12: note: enum is defined here32Enum1 E1_3;33Enum1 E1_4{0};34Enum1 E1_5{Enum1::A};35Enum1 E1_6{Enum1::B};36 37Enum2 E2_1{};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 enumerator39// CHECK-NOTES: :14:6: note: enum is defined here40Enum2 E2_2 = Enum2();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 enumerator42// CHECK-NOTES: :14:6: note: enum is defined here43 44void f1() {45 static Enum1 S; // FIMXE: warn for this?46 Enum1 A;47 Enum1 B = Enum1();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 enumerator49 // CHECK-NOTES: :9:12: note: enum is defined here50 int C = int();51}52 53void f2() {54 Enum1 A{};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 enumerator56 // CHECK-NOTES: :9:12: note: enum is defined here57 Enum1 B = Enum1();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 enumerator59 // CHECK-NOTES: :9:12: note: enum is defined here60 Enum1 C[5] = {{}};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 enumerator62 // CHECK-NOTES: :9:12: note: enum is defined here63 // CHECK-NOTES: :[[@LINE-3]]:17: warning: enum value of type 'Enum1' initialized with invalid value of 0, enum doesn't have a zero-value enumerator64 // CHECK-NOTES: :9:12: note: enum is defined here65 Enum1 D[5] = {}; // FIMXE: warn for this?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 enumerator67 // CHECK-NOTES: :9:12: note: enum is defined here68}69 70struct S1 {71 Enum1 E_1{};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 enumerator73 // CHECK-NOTES: :9:12: note: enum is defined here74 Enum1 E_2 = Enum1();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 enumerator76 // CHECK-NOTES: :9:12: note: enum is defined here77 Enum1 E_3;78 Enum1 E_4;79 Enum1 E_5;80 81 S1() :82 E_3{},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 enumerator84 // CHECK-NOTES: :9:12: note: enum is defined here85 E_4(),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 enumerator87 // CHECK-NOTES: :9:12: note: enum is defined here88 E_5{Enum1::B}89 {}90};91 92struct S2 {93 Enum0 X;94 Enum1 Y;95 Enum2 Z;96};97 98struct S3 {99 S2 X;100 int Y;101};102 103struct S4 : public S3 {104 int Z;105};106 107struct S5 {108 S2 X[3];109 int Y;110};111 112S2 VarS2{};113// CHECK-NOTES: :[[@LINE-1]]:9: warning: enum value of type 'Enum1' initialized with invalid value of 0114// CHECK-NOTES: :9:12: note: enum is defined here115// CHECK-NOTES: :[[@LINE-3]]:9: warning: enum value of type 'Enum2' initialized with invalid value of 0116// CHECK-NOTES: :14:6: note: enum is defined here117S3 VarS3{};118// CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0119// CHECK-NOTES: :9:12: note: enum is defined here120// CHECK-NOTES: :[[@LINE-3]]:10: warning: enum value of type 'Enum2' initialized with invalid value of 0121// CHECK-NOTES: :14:6: note: enum is defined here122S4 VarS4{};123// CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0124// CHECK-NOTES: :9:12: note: enum is defined here125// CHECK-NOTES: :[[@LINE-3]]:10: warning: enum value of type 'Enum2' initialized with invalid value of 0126// CHECK-NOTES: :14:6: note: enum is defined here127S5 VarS5{};128// CHECK-NOTES: :[[@LINE-1]]:10: warning: enum value of type 'Enum1' initialized with invalid value of 0129// CHECK-NOTES: :9:12: note: enum is defined here130 131enum class EnumFwd;132 133EnumFwd Fwd{};134 135enum class EnumEmpty {};136 137EnumEmpty Empty{};138 139template<typename T>140struct Templ {141 T Mem1{};142 // CHECK-NOTES: :[[@LINE-1]]:9: warning: enum value of type 'Enum1' initialized with invalid value of 0143 // CHECK-NOTES: :9:12: note: enum is defined here144};145 146Templ<Enum1> TemplVar;147 148enum MyEnum {149 A = 1,150 B151};152 153MyEnum 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 enumerator155// CHECK-NOTES-DEFAULT: :148:6: note: enum is defined here156 157namespace std {158 enum errc {159 A = 1,160 B161 };162}163 164std::errc err{};165