Skip to content

Commit af4e6e1

Browse files
static_cast over C-style cast
1 parent 290ddad commit af4e6e1

File tree

8 files changed

+59
-43
lines changed

8 files changed

+59
-43
lines changed

libc/test/include/complex_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ TEST(LlvmLibcComplexTest, CMPLXMacro) {
2727
EXPECT_CFP_EQ(CMPLXL(1.0l, 0), 1.0l);
2828

2929
#ifdef LIBC_TYPES_HAS_CFLOAT16
30-
EXPECT_CFP_EQ(CMPLXF16(0, 1.0), (_Complex _Float16)I);
31-
EXPECT_CFP_EQ(CMPLXF16(1.0, 0), (_Complex _Float16)1.0);
30+
EXPECT_CFP_EQ(CMPLXF16(0, 1.0), static_cast<_Complex _Float16>(I));
31+
EXPECT_CFP_EQ(CMPLXF16(1.0, 0), static_cast<_Complex _Float16>(1.0));
3232
#endif // LIBC_TYPES_HAS_CFLOAT16
3333

3434
#ifdef LIBC_TYPES_HAS_CFLOAT128

libc/test/src/signal/sigaltstack_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void handler(int) {
3333
// out or mapped to a register.
3434
uint8_t var[LOCAL_VAR_SIZE];
3535
for (int i = 0; i < LOCAL_VAR_SIZE; ++i)
36-
var[i] = (uint8_t)i;
36+
var[i] = static_cast<uint8_t>(i);
3737
// Verify that array is completely on the alt_stack.
3838
for (int i = 0; i < LOCAL_VAR_SIZE; ++i) {
3939
if (!(uintptr_t(var + i) < uintptr_t(alt_stack + ALT_STACK_SIZE) &&

libc/test/src/stdio/fileop_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ TEST_F(LlvmLibcFILETest, SimpleFileOperations) {
101101

102102
// This is not a readable file.
103103
ASSERT_THAT(LIBC_NAMESPACE::fread(data, 1, 1, file),
104-
returns(EQ(size_t(0))).with_errno(NE(0)));
104+
returns(EQ(static_cast<size_t>(0))).with_errno(NE(0)));
105105

106106
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
107107

@@ -175,7 +175,7 @@ TEST_F(LlvmLibcFILETest, FOpenFWriteSizeGreaterThanOne) {
175175
// Trying to read more should fetch nothing.
176176
ASSERT_THAT(
177177
LIBC_NAMESPACE::fread(read_data, sizeof(MyStruct), WRITE_NMEMB, file),
178-
returns(EQ(size_t(0))).with_errno(EQ(0)));
178+
returns(EQ(static_cast<size_t>(0))).with_errno(EQ(0)));
179179
EXPECT_NE(LIBC_NAMESPACE::feof(file), 0);
180180
EXPECT_EQ(LIBC_NAMESPACE::ferror(file), 0);
181181
ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);

libc/test/src/stdio/fopen_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TEST(LlvmLibcFOpenTest, PrintToFile) {
2222

2323
static constexpr char STRING[] = "A simple string written to a file\n";
2424
size_t result = LIBC_NAMESPACE::fwrite(STRING, 1, sizeof(STRING) - 1, file);
25-
EXPECT_GE(result, size_t(0));
25+
EXPECT_GE(result, static_cast<size_t>(0));
2626

2727
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
2828

libc/test/src/sys/socket/linux/send_recv_test.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ TEST_F(LlvmLibcSendRecvTest, SendFails) {
5050
const char TEST_MESSAGE[] = "connection terminated";
5151
const size_t MESSAGE_LEN = sizeof(TEST_MESSAGE);
5252

53-
ASSERT_THAT((int)LIBC_NAMESPACE::send(-1, TEST_MESSAGE, MESSAGE_LEN, 0),
54-
Fails(EBADF));
53+
ASSERT_THAT(
54+
static_cast<int>(LIBC_NAMESPACE::send(-1, TEST_MESSAGE, MESSAGE_LEN, 0)),
55+
Fails(EBADF));
5556
}
5657

5758
TEST_F(LlvmLibcSendRecvTest, RecvFails) {
5859
char buffer[256];
5960

60-
ASSERT_THAT((int)LIBC_NAMESPACE::recv(-1, buffer, sizeof(buffer), 0),
61-
Fails(EBADF));
61+
ASSERT_THAT(
62+
static_cast<int>(LIBC_NAMESPACE::recv(-1, buffer, sizeof(buffer), 0)),
63+
Fails(EBADF));
6264
}

libc/test/src/sys/socket/linux/sendmsg_recvmsg_test.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ TEST_F(LlvmLibcSendMsgRecvMsgTest, SucceedsWithSocketPair) {
4545
send_message.msg_controllen = 0;
4646
send_message.msg_flags = 0;
4747

48-
ASSERT_THAT((int)LIBC_NAMESPACE::sendmsg(sockpair[0], &send_message, 0),
49-
Succeeds(static_cast<ssize_t>(MESSAGE_LEN)));
48+
ASSERT_THAT(
49+
static_cast<int>(LIBC_NAMESPACE::sendmsg(sockpair[0], &send_message, 0)),
50+
Succeeds(static_cast<ssize_t>(MESSAGE_LEN)));
5051

5152
char buffer[256];
5253

@@ -63,8 +64,9 @@ TEST_F(LlvmLibcSendMsgRecvMsgTest, SucceedsWithSocketPair) {
6364
recv_message.msg_controllen = 0;
6465
recv_message.msg_flags = 0;
6566

66-
ASSERT_THAT((int)LIBC_NAMESPACE::recvmsg(sockpair[1], &recv_message, 0),
67-
Succeeds(static_cast<ssize_t>(MESSAGE_LEN)));
67+
ASSERT_THAT(
68+
static_cast<int>(LIBC_NAMESPACE::recvmsg(sockpair[1], &recv_message, 0)),
69+
Succeeds(static_cast<ssize_t>(MESSAGE_LEN)));
6870

6971
ASSERT_STREQ(buffer, TEST_MESSAGE);
7072

@@ -91,7 +93,8 @@ TEST_F(LlvmLibcSendMsgRecvMsgTest, SendFails) {
9193
send_message.msg_controllen = 0;
9294
send_message.msg_flags = 0;
9395

94-
ASSERT_THAT((int)LIBC_NAMESPACE::sendmsg(-1, &send_message, 0), Fails(EBADF));
96+
ASSERT_THAT(static_cast<int>(LIBC_NAMESPACE::sendmsg(-1, &send_message, 0)),
97+
Fails(EBADF));
9598
}
9699

97100
TEST_F(LlvmLibcSendMsgRecvMsgTest, RecvFails) {
@@ -110,5 +113,6 @@ TEST_F(LlvmLibcSendMsgRecvMsgTest, RecvFails) {
110113
recv_message.msg_controllen = 0;
111114
recv_message.msg_flags = 0;
112115

113-
ASSERT_THAT((int)LIBC_NAMESPACE::recvmsg(-1, &recv_message, 0), Fails(EBADF));
116+
ASSERT_THAT(static_cast<int>(LIBC_NAMESPACE::recvmsg(-1, &recv_message, 0)),
117+
Fails(EBADF));
114118
}

libc/test/src/sys/socket/linux/sendto_recvfrom_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ TEST_F(LlvmLibcSendToRecvFromTest, SendToFails) {
5454

5555
ASSERT_THAT(
5656
LIBC_NAMESPACE::sendto(-1, TEST_MESSAGE, MESSAGE_LEN, 0, nullptr, 0),
57-
Fails(ssize_t(EBADF), ssize_t(-1)));
57+
Fails(static_cast<ssize_t>(EBADF), static_cast<ssize_t>(-1)));
5858
}
5959

6060
TEST_F(LlvmLibcSendToRecvFromTest, RecvFromFails) {
6161
char buffer[256];
6262

6363
ASSERT_THAT(
6464
LIBC_NAMESPACE::recvfrom(-1, buffer, sizeof(buffer), 0, nullptr, 0),
65-
Fails(ssize_t(EBADF), ssize_t(-1)));
65+
Fails(static_cast<ssize_t>(EBADF), static_cast<ssize_t>(-1)));
6666
}

libc/test/src/time/mktime_test.cpp

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ TEST(LlvmLibcMkTime, FailureSetsErrno) {
3636
.tm_wday = 0,
3737
.tm_yday = 0,
3838
.tm_isdst = 0};
39-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
39+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
40+
Fails(EOVERFLOW));
4041
}
4142

4243
TEST(LlvmLibcMkTime, InvalidSeconds) {
@@ -51,7 +52,8 @@ TEST(LlvmLibcMkTime, InvalidSeconds) {
5152
.tm_wday = 0,
5253
.tm_yday = 0,
5354
.tm_isdst = 0};
54-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Succeeds(-1));
55+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
56+
Succeeds(-1));
5557
EXPECT_TM_EQ((tm{.tm_sec = 59,
5658
.tm_min = 59,
5759
.tm_hour = 23,
@@ -75,7 +77,8 @@ TEST(LlvmLibcMkTime, InvalidSeconds) {
7577
.tm_wday = 0,
7678
.tm_yday = 0,
7779
.tm_isdst = 0};
78-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Succeeds(60));
80+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
81+
Succeeds(60));
7982
EXPECT_TM_EQ((tm{.tm_sec = 0,
8083
.tm_min = 1,
8184
.tm_hour = 0,
@@ -101,7 +104,7 @@ TEST(LlvmLibcMkTime, InvalidMinutes) {
101104
.tm_wday = 0,
102105
.tm_yday = 0,
103106
.tm_isdst = 0};
104-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
107+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
105108
Succeeds(-LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN));
106109
EXPECT_TM_EQ((tm{.tm_sec = 0,
107110
.tm_min = 59,
@@ -126,7 +129,7 @@ TEST(LlvmLibcMkTime, InvalidMinutes) {
126129
.tm_wday = 0,
127130
.tm_yday = 0,
128131
.tm_isdst = 0};
129-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
132+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
130133
Succeeds(60 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN));
131134
EXPECT_TM_EQ((tm{.tm_sec = 0,
132135
.tm_min = 0,
@@ -153,7 +156,7 @@ TEST(LlvmLibcMkTime, InvalidHours) {
153156
.tm_wday = 0,
154157
.tm_yday = 0,
155158
.tm_isdst = 0};
156-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
159+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
157160
Succeeds(-LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR));
158161
EXPECT_TM_EQ((tm{.tm_sec = 0,
159162
.tm_min = 0,
@@ -179,7 +182,7 @@ TEST(LlvmLibcMkTime, InvalidHours) {
179182
.tm_yday = 0,
180183
.tm_isdst = 0};
181184
EXPECT_THAT(
182-
(int)LIBC_NAMESPACE::mktime(&tm_data),
185+
static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
183186
Succeeds(24 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR));
184187
EXPECT_TM_EQ((tm{.tm_sec = 0,
185188
.tm_min = 0,
@@ -205,7 +208,7 @@ TEST(LlvmLibcMkTime, InvalidYear) {
205208
.tm_wday = 0,
206209
.tm_yday = 0,
207210
.tm_isdst = 0};
208-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
211+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
209212
Succeeds(-LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR *
210213
LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY));
211214
EXPECT_TM_EQ((tm{.tm_sec = 0,
@@ -234,7 +237,8 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) {
234237
.tm_wday = 0,
235238
.tm_yday = 0,
236239
.tm_isdst = 0};
237-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
240+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
241+
Fails(EOVERFLOW));
238242
}
239243

240244
{
@@ -248,7 +252,8 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) {
248252
.tm_wday = 0,
249253
.tm_yday = 0,
250254
.tm_isdst = 0};
251-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
255+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
256+
Fails(EOVERFLOW));
252257
}
253258

254259
{
@@ -262,7 +267,8 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) {
262267
.tm_wday = 0,
263268
.tm_yday = 0,
264269
.tm_isdst = 0};
265-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
270+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
271+
Fails(EOVERFLOW));
266272
}
267273

268274
{
@@ -276,7 +282,8 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) {
276282
.tm_wday = 0,
277283
.tm_yday = 0,
278284
.tm_isdst = 0};
279-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
285+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
286+
Fails(EOVERFLOW));
280287
}
281288

282289
{
@@ -290,7 +297,8 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) {
290297
.tm_wday = 0,
291298
.tm_yday = 0,
292299
.tm_isdst = 0};
293-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
300+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
301+
Fails(EOVERFLOW));
294302
}
295303

296304
{
@@ -304,7 +312,8 @@ TEST(LlvmLibcMkTime, InvalidEndOf32BitEpochYear) {
304312
.tm_wday = 0,
305313
.tm_yday = 0,
306314
.tm_isdst = 0};
307-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Fails(EOVERFLOW));
315+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
316+
Fails(EOVERFLOW));
308317
}
309318
}
310319

@@ -321,7 +330,7 @@ TEST(LlvmLibcMkTime, InvalidMonths) {
321330
.tm_yday = 0,
322331
.tm_isdst = 0};
323332
EXPECT_THAT(
324-
(int)LIBC_NAMESPACE::mktime(&tm_data),
333+
static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
325334
Succeeds(-32 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY));
326335
EXPECT_TM_EQ((tm{.tm_sec = 0,
327336
.tm_min = 0,
@@ -347,7 +356,7 @@ TEST(LlvmLibcMkTime, InvalidMonths) {
347356
.tm_yday = 0,
348357
.tm_isdst = 0};
349358
EXPECT_THAT(
350-
(int)LIBC_NAMESPACE::mktime(&tm_data),
359+
static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
351360
Succeeds(LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR *
352361
LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY));
353362
EXPECT_TM_EQ((tm{.tm_sec = 0,
@@ -375,7 +384,7 @@ TEST(LlvmLibcMkTime, InvalidDays) {
375384
.tm_wday = 0,
376385
.tm_yday = 0,
377386
.tm_isdst = 0};
378-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
387+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
379388
Succeeds(-1 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY));
380389
EXPECT_TM_EQ((tm{.tm_sec = 0,
381390
.tm_min = 0,
@@ -400,7 +409,7 @@ TEST(LlvmLibcMkTime, InvalidDays) {
400409
.tm_wday = 0,
401410
.tm_yday = 0,
402411
.tm_isdst = 0};
403-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
412+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
404413
Succeeds(31 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY));
405414
EXPECT_TM_EQ((tm{.tm_sec = 0,
406415
.tm_min = 0,
@@ -425,7 +434,7 @@ TEST(LlvmLibcMkTime, InvalidDays) {
425434
.tm_wday = 0,
426435
.tm_yday = 0,
427436
.tm_isdst = 0};
428-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
437+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
429438
Succeeds(59 * LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY));
430439
EXPECT_TM_EQ((tm{.tm_sec = 0,
431440
.tm_min = 0,
@@ -451,7 +460,7 @@ TEST(LlvmLibcMkTime, InvalidDays) {
451460
.tm_yday = 0,
452461
.tm_isdst = 0};
453462
EXPECT_THAT(
454-
(int)LIBC_NAMESPACE::mktime(&tm_data),
463+
static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
455464
Succeeds(((2 * LIBC_NAMESPACE::time_constants::DAYS_PER_NON_LEAP_YEAR) +
456465
60) *
457466
LIBC_NAMESPACE::time_constants::SECONDS_PER_DAY));
@@ -481,7 +490,8 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) {
481490
.tm_wday = 0,
482491
.tm_yday = 0,
483492
.tm_isdst = 0};
484-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data), Succeeds(0x7FFFFFFF));
493+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
494+
Succeeds(0x7FFFFFFF));
485495
EXPECT_TM_EQ((tm{.tm_sec = 7,
486496
.tm_min = 14,
487497
.tm_hour = 3,
@@ -507,7 +517,7 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) {
507517
.tm_wday = 0,
508518
.tm_yday = 0,
509519
.tm_isdst = 0};
510-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
520+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
511521
Succeeds(0x7FFFFFFF - 8));
512522
EXPECT_TM_EQ((tm{.tm_sec = 59,
513523
.tm_min = 13,
@@ -533,7 +543,7 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) {
533543
.tm_wday = 0,
534544
.tm_yday = 0,
535545
.tm_isdst = 0};
536-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
546+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
537547
Succeeds(0x7FFFFFFF - 8 -
538548
14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN));
539549
EXPECT_TM_EQ((tm{.tm_sec = 59,
@@ -560,7 +570,7 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) {
560570
.tm_wday = 0,
561571
.tm_yday = 0,
562572
.tm_isdst = 0};
563-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
573+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
564574
Succeeds(0x7FFFFFFF - 8 -
565575
14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN -
566576
3 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR));
@@ -588,7 +598,7 @@ TEST(LlvmLibcMkTime, EndOf32BitEpochYear) {
588598
.tm_wday = 0,
589599
.tm_yday = 0,
590600
.tm_isdst = 0};
591-
EXPECT_THAT((int)LIBC_NAMESPACE::mktime(&tm_data),
601+
EXPECT_THAT(static_cast<int>(LIBC_NAMESPACE::mktime(&tm_data)),
592602
Succeeds(0x7FFFFFFF - 8 -
593603
14 * LIBC_NAMESPACE::time_constants::SECONDS_PER_MIN -
594604
3 * LIBC_NAMESPACE::time_constants::SECONDS_PER_HOUR -

0 commit comments

Comments
 (0)