11#include < cstdint>
22
33struct A {
4- explicit A (bool ) {}
4+ explicit A (bool ) {}
55};
66
77struct BitField {
8- std::uint8_t bit : 1 ;
8+ std::uint8_t bit : 1 ;
99};
1010
1111void f1 (std::int32_t n) {}
1212void f2 (double d) {}
1313
1414void test_bool_conversion_violations () {
15- bool b1 = true ;
16- bool b2 = false ;
17- double d1 = 1.0 ;
18- std::int8_t s8a = 0 ;
19- std::int32_t s32a = 0 ;
20- BitField bf;
21-
22- // Bitwise operations - non-compliant
23- if (b1 & b2) {} // NON_COMPLIANT
24- if (b1 | b2) {} // NON_COMPLIANT
25- if (b1 ^ b2) {} // NON_COMPLIANT
26- if (~b1) {} // NON_COMPLIANT
27-
28- // Relational operations - non-compliant
29- if (b1 < b2) {} // NON_COMPLIANT
30- if (b1 > b2) {} // NON_COMPLIANT
31- if (b1 <= b2) {} // NON_COMPLIANT
32- if (b1 >= b2) {} // NON_COMPLIANT
33-
34- // Comparison with integer literals - non-compliant
35- if (b1 == 0 ) {} // NON_COMPLIANT
36- if (b1 == 1 ) {} // NON_COMPLIANT
37- if (b1 != 0 ) {} // NON_COMPLIANT
38-
39- // Arithmetic operations - non-compliant
40- double l1 = d1 * b1; // NON_COMPLIANT
41- double l2 = d1 + b1; // NON_COMPLIANT
42- std::int32_t l3 = s32a + b1; // NON_COMPLIANT
43-
44- // Explicit casts to integral types - non-compliant
45- s8a = static_cast <std::int8_t >(b1); // NON_COMPLIANT
46- s32a = static_cast <std::int32_t >(b1); // NON_COMPLIANT
47-
48- // Function parameter conversion - non-compliant
49- f1 (b1); // NON_COMPLIANT
50- f2 (b1); // NON_COMPLIANT
51-
52- // Switch statement - non-compliant
53- switch (b1) { // NON_COMPLIANT
54- case 0 : break ;
55- case 1 : break ;
56- }
57-
58- // Assignment to integral types - non-compliant
59- s8a = b1; // NON_COMPLIANT
60- s32a = b1; // NON_COMPLIANT
61- d1 = b1; // NON_COMPLIANT
15+ bool b1 = true ;
16+ bool b2 = false ;
17+ double d1 = 1.0 ;
18+ std::int8_t s8a = 0 ;
19+ std::int32_t s32a = 0 ;
20+ BitField bf;
21+
22+ // Bitwise operations - non-compliant
23+ if (b1 & b2) { // NON_COMPLIANT
24+ }
25+ if (b1 | b2) { // NON_COMPLIANT
26+ }
27+ if (b1 ^ b2) { // NON_COMPLIANT
28+ }
29+ if (~b1) { // NON_COMPLIANT
30+ }
31+
32+ // Relational operations - non-compliant
33+ if (b1 < b2) { // NON_COMPLIANT
34+ }
35+ if (b1 > b2) { // NON_COMPLIANT
36+ }
37+ if (b1 <= b2) { // NON_COMPLIANT
38+ }
39+ if (b1 >= b2) { // NON_COMPLIANT
40+ }
41+
42+ // Comparison with integer literals - non-compliant
43+ if (b1 == 0 ) { // NON_COMPLIANT
44+ }
45+ if (b1 == 1 ) { // NON_COMPLIANT
46+ }
47+ if (b1 != 0 ) { // NON_COMPLIANT
48+ }
49+
50+ // Arithmetic operations - non-compliant
51+ double l1 = d1 * b1; // NON_COMPLIANT
52+ double l2 = d1 + b1; // NON_COMPLIANT
53+ std::int32_t l3 = s32a + b1; // NON_COMPLIANT
54+
55+ // Explicit casts to integral types - non-compliant
56+ s8a = static_cast <std::int8_t >(b1); // NON_COMPLIANT
57+ s32a = static_cast <std::int32_t >(b1); // NON_COMPLIANT
58+
59+ // Function parameter conversion - non-compliant
60+ f1 (b1); // NON_COMPLIANT
61+ f2 (b1); // NON_COMPLIANT
62+
63+ // Switch statement - non-compliant
64+ switch (b1) { // NON_COMPLIANT
65+ case 0 :
66+ break ;
67+ case 1 :
68+ break ;
69+ }
70+
71+ // Assignment to integral types - non-compliant
72+ s8a = b1; // NON_COMPLIANT
73+ s32a = b1; // NON_COMPLIANT
74+ d1 = b1; // NON_COMPLIANT
6275}
6376
6477void test_bool_conversion_compliant () {
65- bool b1 = true ;
66- bool b2 = false ;
67- std::int8_t s8a = 0 ;
68- BitField bf;
69-
70- // Boolean equality operations - compliant
71- if (b1 == false ) {} // COMPLIANT
72- if (b1 == true ) {} // COMPLIANT
73- if (b1 == b2) {} // COMPLIANT
74- if (b1 != b2) {} // COMPLIANT
75-
76- // Logical operations - compliant
77- if (b1 && b2) {} // COMPLIANT
78- if (b1 || b2) {} // COMPLIANT
79- if (!b1) {} // COMPLIANT
80-
81- // Conditional operator without conversion - compliant
82- s8a = b1 ? 3 : 7 ; // COMPLIANT
83-
84- // Function parameter without conversion - compliant
85- f1 (b1 ? 1 : 0 ); // COMPLIANT
86-
87- // Explicit constructor calls - compliant
88- A l1{true }; // COMPLIANT
89- A l2 (false ); // COMPLIANT
90- A l3 = static_cast <A>(true ); // COMPLIANT
91-
92- // Assignment to constructor - compliant
93- A l4 = A{false }; // COMPLIANT
94-
95- // Bit-field assignment exception - compliant
96- bf.bit = b1; // COMPLIANT
78+ bool b1 = true ;
79+ bool b2 = false ;
80+ std::int8_t s8a = 0 ;
81+ BitField bf;
82+
83+ // Boolean equality operations - compliant
84+ if (b1 == false ) { // COMPLIANT
85+ }
86+ if (b1 == true ) { // COMPLIANT
87+ }
88+ if (b1 == b2) { // COMPLIANT
89+ }
90+ if (b1 != b2) { // COMPLIANT
91+ }
92+
93+ // Logical operations - compliant
94+ if (b1 && b2) { // COMPLIANT
95+ }
96+ if (b1 || b2) { // COMPLIANT
97+ }
98+ if (!b1) { // COMPLIANT
99+ }
100+
101+ // Conditional operator without conversion - compliant
102+ s8a = b1 ? 3 : 7 ; // COMPLIANT
103+
104+ // Function parameter without conversion - compliant
105+ f1 (b1 ? 1 : 0 ); // COMPLIANT
106+
107+ // Explicit constructor calls - compliant
108+ A l1{true }; // COMPLIANT
109+ A l2 (false ); // COMPLIANT
110+ A l3 = static_cast <A>(true ); // COMPLIANT
111+
112+ // Assignment to constructor - compliant
113+ A l4 = A{false }; // COMPLIANT
114+
115+ // Bit-field assignment exception - compliant
116+ bf.bit = b1; // COMPLIANT
97117}
0 commit comments