@@ -9,138 +9,136 @@ std::int8_t g5 = -5;
99std::int32_t g6 = -1000 ;
1010float g7 = 3 .14f ;
1111
12- constexpr std::int32_t f1 (std::int32_t i) {
13- return i * i;
14- }
12+ constexpr std::int32_t f1 (std::int32_t i) { return i * i; }
1513
1614void test_binary_arithmetic_operations () {
17- std::uint8_t l1 = 5 ;
18- std::uint8_t l2 = 10 ;
19- std::uint16_t l3 = 100 ;
20- std::uint32_t l4 = 1000 ;
21- std::int8_t l5 = -5 ;
22- std::int32_t l6 = -1000 ;
23-
24- l1 + l2; // NON_COMPLIANT - u8 + u8 -> signed int
25- l1 * l2; // NON_COMPLIANT - u8 * u8 -> signed int
26- l1 - l2; // NON_COMPLIANT - u8 - u8 -> signed int
27- l1 / l2; // NON_COMPLIANT - u8 / u8 -> signed int
28- l1 % l2; // NON_COMPLIANT - u8 % u8 -> signed int
29- l1 & l2; // NON_COMPLIANT - u8 & u8 -> signed int
30- l1 | l2; // NON_COMPLIANT - u8 | u8 -> signed int
31- l1 ^ l2; // NON_COMPLIANT - u8 ^ u8 -> signed int
32-
33- static_cast <std::uint32_t >(l1) + l2; // COMPLIANT - l2 -> unsigned int
34- l1 + static_cast <std::uint32_t >(l2); // COMPLIANT - l1 -> unsigned int
35-
36- l6 * l5; // COMPLIANT - l5 -> signed int
37- l4 / l1; // COMPLIANT - l1 -> unsigned int
15+ std::uint8_t l1 = 5 ;
16+ std::uint8_t l2 = 10 ;
17+ std::uint16_t l3 = 100 ;
18+ std::uint32_t l4 = 1000 ;
19+ std::int8_t l5 = -5 ;
20+ std::int32_t l6 = -1000 ;
21+
22+ l1 + l2; // NON_COMPLIANT - u8 + u8 -> signed int
23+ l1 *l2; // NON_COMPLIANT - u8 * u8 -> signed int
24+ l1 - l2; // NON_COMPLIANT - u8 - u8 -> signed int
25+ l1 / l2; // NON_COMPLIANT - u8 / u8 -> signed int
26+ l1 % l2; // NON_COMPLIANT - u8 % u8 -> signed int
27+ l1 & l2; // NON_COMPLIANT - u8 & u8 -> signed int
28+ l1 | l2; // NON_COMPLIANT - u8 | u8 -> signed int
29+ l1 ^ l2; // NON_COMPLIANT - u8 ^ u8 -> signed int
30+
31+ static_cast <std::uint32_t >(l1) + l2; // COMPLIANT - l2 -> unsigned int
32+ l1 + static_cast <std::uint32_t >(l2); // COMPLIANT - l1 -> unsigned int
33+
34+ l6 *l5; // COMPLIANT - l5 -> signed int
35+ l4 / l1; // COMPLIANT - l1 -> unsigned int
3836}
3937
4038void test_assignment_operations () {
41- std::uint8_t l1 = 5 ;
42- std::uint8_t l2 = 10 ;
43- std::uint32_t l3 = 1000 ;
44-
45- l1 += l2; // NON_COMPLIANT - same as l1 + l2
46- l1 -= l2; // NON_COMPLIANT - same as l1 - l2
47- l1 *= l2; // NON_COMPLIANT - same as l1 * l2
48- l1 /= l2; // NON_COMPLIANT - same as l1 / l2
49- l1 %= l2; // NON_COMPLIANT - same as l1 % l2
50- l1 &= l2; // NON_COMPLIANT - same as l1 & l2
51- l1 |= l2; // NON_COMPLIANT - same as l1 | l2
52- l1 ^= l2; // NON_COMPLIANT - same as l1 ^ l2
53-
54- l1 += static_cast <std::uint32_t >(l2); // COMPLIANT - l1 -> unsigned int
55- l1 += l3; // COMPLIANT - l1 -> unsigned int
39+ std::uint8_t l1 = 5 ;
40+ std::uint8_t l2 = 10 ;
41+ std::uint32_t l3 = 1000 ;
42+
43+ l1 += l2; // NON_COMPLIANT - same as l1 + l2
44+ l1 -= l2; // NON_COMPLIANT - same as l1 - l2
45+ l1 *= l2; // NON_COMPLIANT - same as l1 * l2
46+ l1 /= l2; // NON_COMPLIANT - same as l1 / l2
47+ l1 %= l2; // NON_COMPLIANT - same as l1 % l2
48+ l1 &= l2; // NON_COMPLIANT - same as l1 & l2
49+ l1 |= l2; // NON_COMPLIANT - same as l1 | l2
50+ l1 ^= l2; // NON_COMPLIANT - same as l1 ^ l2
51+
52+ l1 += static_cast <std::uint32_t >(l2); // COMPLIANT - l1 -> unsigned int
53+ l1 += l3; // COMPLIANT - l1 -> unsigned int
5654}
5755
5856void test_comparison_operations () {
59- std::int32_t l1 = -1000 ;
60- std::uint32_t l2 = 1000 ;
61- std::uint8_t l3 = 5 ;
62- std::uint16_t l4 = 100 ;
63-
64- l1 > l2; // NON_COMPLIANT - l1 -> unsigned int
65- l1 < l2; // NON_COMPLIANT - l1 -> unsigned int
66- l1 >= l2; // NON_COMPLIANT - l1 -> unsigned int
67- l1 <= l2; // NON_COMPLIANT - l1 -> unsigned int
68- l1 == l2; // NON_COMPLIANT - l1 -> unsigned int
69- l1 != l2; // NON_COMPLIANT - l1 -> unsigned int
70-
71- l3 > l4; // NON_COMPLIANT - l3 and l4 -> signed int
72- l3 < l4; // NON_COMPLIANT - l3 and l4 -> signed int
57+ std::int32_t l1 = -1000 ;
58+ std::uint32_t l2 = 1000 ;
59+ std::uint8_t l3 = 5 ;
60+ std::uint16_t l4 = 100 ;
61+
62+ l1 > l2; // NON_COMPLIANT - l1 -> unsigned int
63+ l1 < l2; // NON_COMPLIANT - l1 -> unsigned int
64+ l1 >= l2; // NON_COMPLIANT - l1 -> unsigned int
65+ l1 <= l2; // NON_COMPLIANT - l1 -> unsigned int
66+ l1 == l2; // NON_COMPLIANT - l1 -> unsigned int
67+ l1 != l2; // NON_COMPLIANT - l1 -> unsigned int
68+
69+ l3 > l4; // NON_COMPLIANT - l3 and l4 -> signed int
70+ l3 < l4; // NON_COMPLIANT - l3 and l4 -> signed int
7371}
7472
7573void test_conditional_operator () {
76- bool l1 = true ;
77- std::uint8_t l2 = 5 ;
78- std::uint8_t l3 = 10 ;
79- std::uint16_t l4 = 100 ;
80-
81- l1 ? l2 : l3; // COMPLIANT - no conversion
82- l1 ? l2 : l4; // NON_COMPLIANT - l2 and l4 -> signed int
74+ bool l1 = true ;
75+ std::uint8_t l2 = 5 ;
76+ std::uint8_t l3 = 10 ;
77+ std::uint16_t l4 = 100 ;
78+
79+ l1 ? l2 : l3; // COMPLIANT - no conversion
80+ l1 ? l2 : l4; // NON_COMPLIANT - l2 and l4 -> signed int
8381}
8482
8583void test_shift_operations () {
86- std::uint8_t l1 = 5 ;
87- std::uint32_t l2 = 1000 ;
88-
89- l1 << 2 ; // NON_COMPLIANT - l1 -> signed int
90- l1 >> 1 ; // NON_COMPLIANT - l1 -> signed int
91- l2 << 2 ; // COMPLIANT
92- l2 >> 1 ; // COMPLIANT
84+ std::uint8_t l1 = 5 ;
85+ std::uint32_t l2 = 1000 ;
86+
87+ l1 << 2 ; // NON_COMPLIANT - l1 -> signed int
88+ l1 >> 1 ; // NON_COMPLIANT - l1 -> signed int
89+ l2 << 2 ; // COMPLIANT
90+ l2 >> 1 ; // COMPLIANT
9391}
9492
9593void test_unary_operations () {
96- std::uint8_t l1 = 5 ;
97- std::uint32_t l2 = 1000 ;
98- std::int8_t l3 = -5 ;
99-
100- ~l1; // NON_COMPLIANT - l1 -> signed int
101- ~l2; // COMPLIANT
102- -l1; // NON_COMPLIANT - l1 -> signed int
103- -l3; // COMPLIANT - l3 is signed
104- +l1; // NON_COMPLIANT - l1 -> signed int
94+ std::uint8_t l1 = 5 ;
95+ std::uint32_t l2 = 1000 ;
96+ std::int8_t l3 = -5 ;
97+
98+ ~l1; // NON_COMPLIANT - l1 -> signed int
99+ ~l2; // COMPLIANT
100+ -l1; // NON_COMPLIANT - l1 -> signed int
101+ -l3; // COMPLIANT - l3 is signed
102+ +l1; // NON_COMPLIANT - l1 -> signed int
105103}
106104
107105void test_increment_decrement () {
108- std::uint8_t l1 = 5 ;
109- std::uint16_t l2 = 100 ;
110-
111- l1++; // COMPLIANT - rule does not apply
112- ++l1; // COMPLIANT - rule does not apply
113- l1--; // COMPLIANT - rule does not apply
114- --l1; // COMPLIANT - rule does not apply
115- l2++; // COMPLIANT - rule does not apply
116- ++l2; // COMPLIANT - rule does not apply
106+ std::uint8_t l1 = 5 ;
107+ std::uint16_t l2 = 100 ;
108+
109+ l1++; // COMPLIANT - rule does not apply
110+ ++l1; // COMPLIANT - rule does not apply
111+ l1--; // COMPLIANT - rule does not apply
112+ --l1; // COMPLIANT - rule does not apply
113+ l2++; // COMPLIANT - rule does not apply
114+ ++l2; // COMPLIANT - rule does not apply
117115}
118116
119117void test_array_subscript () {
120- int l1[10 ];
121- std::uint8_t l2 = 5 ;
122-
123- l1[l2]; // COMPLIANT - rule does not apply
118+ int l1[10 ];
119+ std::uint8_t l2 = 5 ;
120+
121+ l1[l2]; // COMPLIANT - rule does not apply
124122}
125123
126124void test_exception_compile_time_constants () {
127- std::uint32_t l1 = 1000 ;
128- float l2 = 3 .14f ;
129- std::int32_t l3 = 5 ;
130-
131- l1 - 1 ; // COMPLIANT - exception #1
132- l1 + 42 ; // COMPLIANT - exception #1
133- l2 += 1 ; // COMPLIANT - exception #2
134- l2 += 0x10001 ; // COMPLIANT - exception #2
135- l3 + f1 (10 ); // COMPLIANT - exception #1
136- l2 + f1 (10 ); // COMPLIANT - exception #2
125+ std::uint32_t l1 = 1000 ;
126+ float l2 = 3 .14f ;
127+ std::int32_t l3 = 5 ;
128+
129+ l1 - 1 ; // COMPLIANT - exception #1
130+ l1 + 42 ; // COMPLIANT - exception #1
131+ l2 += 1 ; // COMPLIANT - exception #2
132+ l2 += 0x10001 ; // COMPLIANT - exception #2
133+ l3 + f1 (10 ); // COMPLIANT - exception #1
134+ l2 + f1 (10 ); // COMPLIANT - exception #2
137135}
138136
139137void test_floating_point_conversions () {
140- float l1;
141- std::uint32_t l2;
142-
143- l1 += l2; // NON_COMPLIANT - l2 -> floating
144- l1 *= l2; // NON_COMPLIANT - l2 -> floating
145- l1 /= l2; // NON_COMPLIANT - l2 -> floating
138+ float l1;
139+ std::uint32_t l2;
140+
141+ l1 += l2; // NON_COMPLIANT - l2 -> floating
142+ l1 *= l2; // NON_COMPLIANT - l2 -> floating
143+ l1 /= l2; // NON_COMPLIANT - l2 -> floating
146144}
0 commit comments