1 // RUN: %check_clang_tidy %s cppcoreguidelines-pro-type-union-access %t 2 3 union U { 4 bool union_member1; 5 char union_member2; 6 } u; 7 8 struct S { 9 int non_union_member; 10 union { 11 bool union_member; 12 }; 13 union { 14 char union_member2; 15 } u; 16 } s; 17 18 19 void f(char); 20 void f2(U); 21 void f3(U&); 22 void f4(U*); 23 check()24void check() 25 { 26 u.union_member1 = true; 27 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access] 28 auto b = u.union_member2; 29 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not access members of unions; use (boost::)variant instead 30 auto a = &s.union_member; 31 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: do not access members of unions; use (boost::)variant instead 32 f(s.u.union_member2); 33 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not access members of unions; use (boost::)variant instead 34 35 s.non_union_member = 2; // OK 36 37 U u2 = u; // OK 38 f2(u); // OK 39 f3(u); // OK 40 f4(&u); // OK 41 } 42