@@ -13,15 +13,30 @@ std::int64_t s64;
1313float f;
1414double d;
1515
16+ namespace TestNamespace {
17+ std::uint8_t g1;
18+ std::uint16_t g2;
19+ } // namespace TestNamespace
20+
21+ struct TestStruct {
22+ std::uint8_t m1;
23+ std::uint16_t m2;
24+ static std::uint8_t s1;
25+ static std::uint16_t s2;
26+ };
27+
28+ std::uint8_t TestStruct::s1;
29+ std::uint16_t TestStruct::s2;
30+
1631// Test basic constant assignments
1732void test_constant_assignments () {
18- u32 = 1 ; // COMPLIANT
19- s32 = 4u * 2u ; // COMPLIANT
20- u8 = 3u ; // COMPLIANT
21- u8 = 300u ; // NON_COMPLIANT
33+ u32 = 1 ; // COMPLIANT
34+ s32 = 4u * 2u ; // COMPLIANT
35+ u8 = 3u ; // COMPLIANT
36+ u8 = 300u ; // NON_COMPLIANT
2237 f = 1 ; // COMPLIANT
2338 f = 9999999999 ; // COMPLIANT
24- d = 0 .0f ; // NON_COMPLIANT
39+ d = 0 .0f ; // NON_COMPLIANT
2540 f = 0 .0f ; // COMPLIANT
2641}
2742
@@ -33,7 +48,7 @@ void test_signedness_violations() {
3348
3449// Test size violations
3550void test_size_violations () {
36- u8 = u16 ; // NON_COMPLIANT
51+ u8 = u16 ; // NON_COMPLIANT
3752 u16 = u64 ; // NON_COMPLIANT
3853}
3954
@@ -45,9 +60,49 @@ void test_type_category_violations() {
4560
4661// Test widening of id-expressions
4762void test_widening_id_expressions () {
48- u32 = u8 ; // COMPLIANT
49- s64 = s8; // COMPLIANT
50- u64 = u16 ; // COMPLIANT
63+ u32 = u8 ; // COMPLIANT - widening of id-expression
64+ s64 = s8; // COMPLIANT - widening of id-expression
65+ u64 = u16 ; // COMPLIANT - widening of id-expression
66+ }
67+
68+ // Test widening with namespace qualifiers (allowed)
69+ void test_widening_namespace_qualified () {
70+ u32 = TestNamespace::g1; // COMPLIANT - namespace qualified id-expression
71+ u64 = TestNamespace::g2; // COMPLIANT - namespace qualified id-expression
72+ }
73+
74+ // Test widening with type qualifiers (allowed)
75+ void test_widening_type_qualified () {
76+ u32 = TestStruct::s1; // COMPLIANT - type qualified id-expression
77+ u64 = TestStruct::s2; // COMPLIANT - type qualified id-expression
78+ }
79+
80+ // Test widening with decltype (allowed)
81+ void test_widening_decltype_qualified () {
82+ std::uint8_t l1 = 42 ;
83+ std::uint16_t l2 = 42 ;
84+ u32 = decltype (l1){}; // COMPLIANT - treated as a constant
85+ u64 = decltype (l2){}; // COMPLIANT - treated as a constant
86+ TestStruct l3;
87+ u32 = decltype (l3)::s1; // COMPLIANT - decltype qualified
88+ u64 = decltype (l3)::s2; // COMPLIANT - decltype qualified
89+ }
90+
91+ // Test widening with object member access (not allowed)
92+ void test_widening_object_member_access () {
93+ TestStruct l1;
94+ TestStruct *l2 = &l1;
95+ u32 = l1.m1 ; // NON_COMPLIANT - object member access, not id-expression
96+ u64 = l1.m2 ; // NON_COMPLIANT - object member access, not id-expression
97+ u32 = l2->m1 ; // NON_COMPLIANT - object member access, not id-expression
98+ u64 = l2->m2 ; // NON_COMPLIANT - object member access, not id-expression
99+ }
100+
101+ // Test widening with expressions (not allowed)
102+ void test_widening_expressions () {
103+ u32 = u8 + 0 ; // NON_COMPLIANT - not id-expression
104+ u32 = (u8 ); // NON_COMPLIANT - not id-expression (parenthesized)
105+ u32 = static_cast <std::uint8_t >(u8 ); // NON_COMPLIANT - not id-expression
51106}
52107
53108// Test expression results
@@ -66,7 +121,7 @@ void test_bitfields() {
66121 l1.m1 = 2 ; // COMPLIANT
67122 l1.m1 = 32u ; // NON_COMPLIANT
68123 l1.m1 = u8 ; // COMPLIANT
69- l1.m1 = u16 ; // NON_COMPLIANT
124+ l1.m1 = u16 ; // NON_COMPLIANT
70125}
71126
72127// Test enums
@@ -77,9 +132,9 @@ enum States { enabled, disabled };
77132void test_enums () {
78133 Colour l1 = red;
79134 u8 = red; // COMPLIANT
80- u32 = red; // COMPLIANT
135+ u32 = red; // COMPLIANT
81136 u8 = l1; // NON_COMPLIANT
82- u32 = l1; // COMPLIANT
137+ u32 = l1; // COMPLIANT
83138 u8 = enabled; // COMPLIANT - enabled is not numeric
84139}
85140
@@ -106,7 +161,7 @@ void f3(std::int32_t l1) {}
106161
107162void test_overloaded_functions () {
108163 f3 (s32); // COMPLIANT
109- f3 (s8); // NON_COMPLIANT
164+ f3 (s8); // NON_COMPLIANT
110165 f3 (s64); // COMPLIANT
111166}
112167
@@ -123,7 +178,7 @@ void test_function_pointers() {
123178void f5 (const char *l1, ...) {}
124179
125180void test_variadic_functions () {
126- f5 (" test" , u8 ); // NON_COMPLIANT - will be promoted to `int`
181+ f5 (" test" , u8 ); // NON_COMPLIANT - will be promoted to `int`
127182 f5 (" test" , s32); // COMPLIANT - already `int`, no promotion needed
128183}
129184
@@ -136,8 +191,8 @@ struct A {
136191
137192void A::f7 () {
138193 f6 (u32 , " answer" ); // NON_COMPLIANT - extensible, could call a global
139- // function instead - e.g. `void f6(std::uint32_t l1,
140- // std::string l2)`
194+ // function instead - e.g. `void f6(std::uint32_t l1,
195+ // std::string l2)`
141196 this ->f6 (u32 , " answer" ); // COMPLIANT
142197 this ->f6 (u32 , 42 ); // COMPLIANT
143198}
0 commit comments