bugprone-signed-char-misuse.cpp
7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// RUN: %check_clang_tidy %s bugprone-signed-char-misuse %t
///////////////////////////////////////////////////////////////////
/// Test cases correctly caught by the check.
typedef __SIZE_TYPE__ size_t;
namespace std {
template <typename T, size_t N>
struct array {
T &operator[](size_t n);
T &at(size_t n);
};
} // namespace std
int SimpleVarDeclaration() {
signed char CCharacter = -5;
int NCharacter = CCharacter;
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
return NCharacter;
}
int SimpleAssignment() {
signed char CCharacter = -5;
int NCharacter;
NCharacter = CCharacter;
// CHECK-MESSAGES: [[@LINE-1]]:16: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
return NCharacter;
}
int CStyleCast() {
signed char CCharacter = -5;
int NCharacter;
NCharacter = (int)CCharacter;
// CHECK-MESSAGES: [[@LINE-1]]:21: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
return NCharacter;
}
int StaticCast() {
signed char CCharacter = -5;
int NCharacter;
NCharacter = static_cast<int>(CCharacter);
// CHECK-MESSAGES: [[@LINE-1]]:33: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
return NCharacter;
}
int FunctionalCast() {
signed char CCharacter = -5;
int NCharacter;
NCharacter = int(CCharacter);
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
return NCharacter;
}
int NegativeConstValue() {
const signed char CCharacter = -5;
int NCharacter = CCharacter;
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
return NCharacter;
}
int CharPointer(signed char *CCharacter) {
int NCharacter = *CCharacter;
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
return NCharacter;
}
int SignedUnsignedCharEquality(signed char SCharacter) {
unsigned char USCharacter = 'a';
if (SCharacter == USCharacter) // CHECK-MESSAGES: [[@LINE]]:7: warning: comparison between 'signed char' and 'unsigned char' [bugprone-signed-char-misuse]
return 1;
return 0;
}
int SignedUnsignedCharIneqiality(signed char SCharacter) {
unsigned char USCharacter = 'a';
if (SCharacter != USCharacter) // CHECK-MESSAGES: [[@LINE]]:7: warning: comparison between 'signed char' and 'unsigned char' [bugprone-signed-char-misuse]
return 1;
return 0;
}
int CompareWithNonAsciiConstant(unsigned char USCharacter) {
const signed char SCharacter = -5;
if (USCharacter == SCharacter) // CHECK-MESSAGES: [[@LINE]]:7: warning: comparison between 'signed char' and 'unsigned char' [bugprone-signed-char-misuse]
return 1;
return 0;
}
int CompareWithUnsignedNonAsciiConstant(signed char SCharacter) {
const unsigned char USCharacter = 128;
if (USCharacter == SCharacter) // CHECK-MESSAGES: [[@LINE]]:7: warning: comparison between 'signed char' and 'unsigned char' [bugprone-signed-char-misuse]
return 1;
return 0;
}
int SignedCharCArraySubscript(signed char SCharacter) {
int Array[3] = {1, 2, 3};
return Array[static_cast<unsigned int>(SCharacter)]; // CHECK-MESSAGES: [[@LINE]]:42: warning: 'signed char' to 'unsigned int' conversion in array subscript; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
}
int SignedCharSTDArraySubscript(std::array<int, 3> Array, signed char SCharacter) {
return Array[static_cast<unsigned int>(SCharacter)]; // CHECK-MESSAGES: [[@LINE]]:42: warning: 'signed char' to 'unsigned int' conversion in array subscript; consider casting to 'unsigned char' first. [bugprone-signed-char-misuse]
}
///////////////////////////////////////////////////////////////////
/// Test cases correctly ignored by the check.
int UnsignedCharCast() {
unsigned char CCharacter = 'a';
int NCharacter = CCharacter;
return NCharacter;
}
int PositiveConstValue() {
const signed char CCharacter = 5;
int NCharacter = CCharacter;
return NCharacter;
}
// singed char -> integer cast is not the direct child of declaration expression.
int DescendantCast() {
signed char CCharacter = 'a';
int NCharacter = 10 + CCharacter;
return NCharacter;
}
// singed char -> integer cast is not the direct child of assignment expression.
int DescendantCastAssignment() {
signed char CCharacter = 'a';
int NCharacter;
NCharacter = 10 + CCharacter;
return NCharacter;
}
// bool is an integer type in clang; make sure to ignore it.
bool BoolVarDeclaration() {
signed char CCharacter = 'a';
bool BCharacter = CCharacter == 'b';
return BCharacter;
}
// bool is an integer type in clang; make sure to ignore it.
bool BoolAssignment() {
signed char CCharacter = 'a';
bool BCharacter;
BCharacter = CCharacter == 'b';
return BCharacter;
}
// char is an integer type in clang; make sure to ignore it.
unsigned char CharToCharCast() {
signed char SCCharacter = 'a';
unsigned char USCharacter;
USCharacter = SCCharacter;
return USCharacter;
}
int FixComparisonWithSignedCharCast(signed char SCharacter) {
unsigned char USCharacter = 'a';
if (SCharacter == static_cast<signed char>(USCharacter))
return 1;
return 0;
}
int FixComparisonWithUnSignedCharCast(signed char SCharacter) {
unsigned char USCharacter = 'a';
if (static_cast<unsigned char>(SCharacter) == USCharacter)
return 1;
return 0;
}
// Make sure we don't catch other type of char comparison.
int SameCharTypeComparison(signed char SCharacter) {
signed char SCharacter2 = 'a';
if (SCharacter == SCharacter2)
return 1;
return 0;
}
// Make sure we don't catch other type of char comparison.
int SameCharTypeComparison2(unsigned char USCharacter) {
unsigned char USCharacter2 = 'a';
if (USCharacter == USCharacter2)
return 1;
return 0;
}
// Make sure we don't catch integer - char comparison.
int CharIntComparison(signed char SCharacter) {
int ICharacter = 10;
if (SCharacter == ICharacter)
return 1;
return 0;
}
int CompareWithAsciiLiteral(unsigned char USCharacter) {
if (USCharacter == 'x') // no warning
return 1;
return 0;
}
int CompareWithAsciiConstant(unsigned char USCharacter) {
const signed char SCharacter = 'a';
if (USCharacter == SCharacter)
return 1;
return 0;
}
int CompareWithUnsignedAsciiConstant(signed char SCharacter) {
const unsigned char USCharacter = 'a';
if (USCharacter == SCharacter)
return 1;
return 0;
}
int UnsignedCharCArraySubscript(unsigned char USCharacter) {
int Array[3] = {1, 2, 3};
return Array[static_cast<unsigned int>(USCharacter)];
}
int CastedCArraySubscript(signed char SCharacter) {
int Array[3] = {1, 2, 3};
return Array[static_cast<unsigned char>(SCharacter)];
}
int UnsignedCharSTDArraySubscript(std::array<int, 3> Array, unsigned char USCharacter) {
return Array[static_cast<unsigned int>(USCharacter)];
}
int CastedSTDArraySubscript(std::array<int, 3> Array, signed char SCharacter) {
return Array[static_cast<unsigned char>(SCharacter)];
}