Merge pull request #405 from fastfloat/old-style-cast

Avoid old-style casts
This commit is contained in:
Daniel Lemire 2026-08-20 14:00:30 -04:00 committed by GitHub
commit a8a02f7748
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 53 additions and 45 deletions

View File

@ -14,4 +14,4 @@ jobs:
python3 ./script/amalgamate.py > build/fast_float/fast_float.h && python3 ./script/amalgamate.py > build/fast_float/fast_float.h &&
cp tests/string_test.cpp build/ && cp tests/string_test.cpp build/ &&
cd build && cd build &&
g++ string_test.cpp g++ -Werror=old-style-cast string_test.cpp

View File

@ -108,10 +108,10 @@ int main() {
buf.reserve(N * ip_size); buf.reserve(N * ip_size);
for (size_t i = 0; i < N; ++i) { for (size_t i = 0; i < N; ++i) {
uint8_t a = (uint8_t)dist(rng); uint8_t a = static_cast<uint8_t>(dist(rng));
uint8_t b = (uint8_t)dist(rng); uint8_t b = static_cast<uint8_t>(dist(rng));
uint8_t c = (uint8_t)dist(rng); uint8_t c = static_cast<uint8_t>(dist(rng));
uint8_t d = (uint8_t)dist(rng); uint8_t d = static_cast<uint8_t>(dist(rng));
std::string ip_line = make_ip_line(a, b, c, d); std::string ip_line = make_ip_line(a, b, c, d);
ip_line.resize(ip_size, ' '); // pad to fixed size ip_line.resize(ip_size, ' '); // pad to fixed size
buf.append(ip_line); buf.append(ip_line);
@ -127,7 +127,7 @@ int main() {
std::string buffer(ip_size * N, ' '); std::string buffer(ip_size * N, ' ');
pretty_print(volume, bytes, "memcpy baseline", counters::bench([&]() { pretty_print(volume, bytes, "memcpy baseline", counters::bench([&]() {
std::memcpy((char *)buffer.data(), buf.data(), bytes); std::memcpy(buffer.data(), buf.data(), bytes);
})); }));
pretty_print(volume, bytes, "just_seek_ip_end (no parse)", pretty_print(volume, bytes, "just_seek_ip_end (no parse)",
@ -138,7 +138,7 @@ int main() {
int ok = 0; int ok = 0;
for (size_t i = 0; i < N; ++i) { for (size_t i = 0; i < N; ++i) {
const char *q = seek_ip_end(p, pend); const char *q = seek_ip_end(p, pend);
sum += (uint32_t)(q - p); sum += static_cast<uint32_t>(q - p);
p += ip_size; p += ip_size;
} }
sink += sum; sink += sum;

View File

@ -75,7 +75,7 @@ int main() {
buffer.reserve(N * 6); // up to 5 digits + delimiter buffer.reserve(N * 6); // up to 5 digits + delimiter
for (size_t i = 0; i < N; ++i) { for (size_t i = 0; i < N; ++i) {
uint16_t val = (uint16_t)dist(rng); uint16_t val = static_cast<uint16_t>(dist(rng));
expected.push_back(val); expected.push_back(val);
std::string s = std::to_string(val); std::string s = std::to_string(val);
buffer.append(s); buffer.append(s);

View File

@ -32,7 +32,7 @@ template <typename UC> fastfloat_really_inline constexpr bool has_simd_opt() {
// able to optimize it well. // able to optimize it well.
template <typename UC> template <typename UC>
fastfloat_really_inline constexpr bool is_integer(UC c) noexcept { fastfloat_really_inline constexpr bool is_integer(UC c) noexcept {
return (unsigned)(c - UC('0')) <= 9u; return static_cast<unsigned>(c - UC('0')) <= 9u;
} }
fastfloat_really_inline constexpr uint64_t byteswap(uint64_t val) { fastfloat_really_inline constexpr uint64_t byteswap(uint64_t val) {
@ -223,8 +223,8 @@ simd_parse_if_eight_digits_unrolled(char16_t const *chars,
return false; return false;
FASTFLOAT_SIMD_RESTORE_WARNINGS FASTFLOAT_SIMD_RESTORE_WARNINGS
#else #else
(void)chars; static_cast<void>(chars);
(void)i; static_cast<void>(i);
return false; return false;
#endif // FASTFLOAT_SSE2 #endif // FASTFLOAT_SSE2
} }
@ -601,7 +601,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
FASTFLOAT_IF_CONSTEXPR17( FASTFLOAT_IF_CONSTEXPR17(
(std::is_same<T, std::uint8_t>::value && sizeof(UC) == 1)) { (std::is_same<T, std::uint8_t>::value && sizeof(UC) == 1)) {
if (base == 10) { if (base == 10) {
const size_t len = (size_t)(pend - p); const size_t len = static_cast<size_t>(pend - p);
if (len == 0) { if (len == 0) {
if (has_leading_zeros) { if (has_leading_zeros) {
value = 0; value = 0;
@ -646,9 +646,10 @@ parse_int_string(UC const *p, UC const *pend, T &value,
uint32_t magic = uint32_t magic =
((digits + 0x46464646u) | (digits - 0x30303030u)) & 0x80808080u; ((digits + 0x46464646u) | (digits - 0x30303030u)) & 0x80808080u;
uint32_t tz = (uint32_t)countr_zero_32(magic); // 7, 15, 23, 31, or 32 uint32_t tz =
static_cast<uint32_t>(countr_zero_32(magic)); // 7, 15, 23, 31, or 32
uint32_t nd = (tz == 32) ? 4 : (tz >> 3); uint32_t nd = (tz == 32) ? 4 : (tz >> 3);
nd = (uint32_t)(nd < len ? nd : len); nd = static_cast<uint32_t>(nd < len ? nd : len);
if (nd == 0) { if (nd == 0) {
if (has_leading_zeros) { if (has_leading_zeros) {
value = 0; value = 0;
@ -684,7 +685,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
answer.ptr = p + nd; answer.ptr = p + nd;
return answer; return answer;
} }
value = (uint8_t)((0x640a01 * digits) >> 24); value = static_cast<uint8_t>((0x640a01 * digits) >> 24);
answer.ec = std::errc(); answer.ec = std::errc();
answer.ptr = p + nd; answer.ptr = p + nd;
return answer; return answer;

View File

@ -619,8 +619,8 @@ struct bigint : pow5_tables<> {
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
// This is similar to https://github.com/llvm/llvm-project/issues/47746, // This is similar to https://github.com/llvm/llvm-project/issues/47746,
// except the workaround described there don't work here // except the workaround described there don't work here
FASTFLOAT_TRY(small_mul( FASTFLOAT_TRY(small_mul(vec, limb((static_cast<void>(small_power_of_5[0]),
vec, limb(((void)small_power_of_5[0], small_power_of_5[exp])))); small_power_of_5[exp]))));
} }
return true; return true;

View File

@ -400,8 +400,8 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp(
round<T>(answer, [ord](adjusted_mantissa &a, int32_t shift) { round<T>(answer, [ord](adjusted_mantissa &a, int32_t shift) {
round_nearest_tie_even( round_nearest_tie_even(
a, shift, [ord](bool is_odd, bool _, bool __) -> bool { a, shift, [ord](bool is_odd, bool _, bool __) -> bool {
(void)_; // not needed, since we've done our comparison static_cast<void>(_); // not needed, since we've done our comparison
(void)__; // not needed, since we've done our comparison static_cast<void>(__); // not needed, since we've done our comparison
if (ord > 0) { if (ord > 0) {
return true; return true;
} else if (ord < 0) { } else if (ord < 0) {

View File

@ -225,12 +225,16 @@ using parse_options = parse_options_t<char>;
#ifndef FASTFLOAT_ASSERT #ifndef FASTFLOAT_ASSERT
#define FASTFLOAT_ASSERT(x) \ #define FASTFLOAT_ASSERT(x) \
{ ((void)(x)); } { \
static_cast<void>(x); \
}
#endif #endif
#ifndef FASTFLOAT_DEBUG_ASSERT #ifndef FASTFLOAT_DEBUG_ASSERT
#define FASTFLOAT_DEBUG_ASSERT(x) \ #define FASTFLOAT_DEBUG_ASSERT(x) \
{ ((void)(x)); } { \
static_cast<void>(x); \
}
#endif #endif
// rust style `try!()` macro, or `?` operator // rust style `try!()` macro, or `?` operator
@ -509,7 +513,7 @@ leading_zeroes(uint64_t input_num) {
// Search the mask data from most significant bit (MSB) // Search the mask data from most significant bit (MSB)
// to least significant bit (LSB) for a set bit (1). // to least significant bit (LSB) for a set bit (1).
_BitScanReverse64(&leading_zero, input_num); _BitScanReverse64(&leading_zero, input_num);
return (int)(63 - leading_zero); return static_cast<int>(63 - leading_zero);
#else #else
return leading_zeroes_generic(input_num); return leading_zeroes_generic(input_num);
#endif #endif
@ -556,7 +560,7 @@ countr_zero_32(uint32_t input_num) {
#ifdef FASTFLOAT_VISUAL_STUDIO #ifdef FASTFLOAT_VISUAL_STUDIO
unsigned long trailing_zero = 0; unsigned long trailing_zero = 0;
if (_BitScanForward(&trailing_zero, input_num)) { if (_BitScanForward(&trailing_zero, input_num)) {
return (int)trailing_zero; return static_cast<int>(trailing_zero);
} }
return 32; return 32;
#else #else
@ -566,18 +570,21 @@ countr_zero_32(uint32_t input_num) {
// slow emulation routine for 32-bit // slow emulation routine for 32-bit
fastfloat_really_inline constexpr uint64_t emulu(uint32_t x, uint32_t y) { fastfloat_really_inline constexpr uint64_t emulu(uint32_t x, uint32_t y) {
return x * (uint64_t)y; return x * static_cast<uint64_t>(y);
} }
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint64_t fastfloat_really_inline FASTFLOAT_CONSTEXPR14 uint64_t
umul128_generic(uint64_t ab, uint64_t cd, uint64_t *hi) { umul128_generic(uint64_t ab, uint64_t cd, uint64_t *hi) {
uint64_t ad = emulu((uint32_t)(ab >> 32), (uint32_t)cd); uint64_t ad =
uint64_t bd = emulu((uint32_t)ab, (uint32_t)cd); emulu(static_cast<uint32_t>(ab >> 32), static_cast<uint32_t>(cd));
uint64_t adbc = ad + emulu((uint32_t)ab, (uint32_t)(cd >> 32)); uint64_t bd = emulu(static_cast<uint32_t>(ab), static_cast<uint32_t>(cd));
uint64_t adbc_carry = (uint64_t)(adbc < ad); uint64_t adbc =
ad + emulu(static_cast<uint32_t>(ab), static_cast<uint32_t>(cd >> 32));
uint64_t adbc_carry = static_cast<uint64_t>(adbc < ad);
uint64_t lo = bd + (adbc << 32); uint64_t lo = bd + (adbc << 32);
*hi = emulu((uint32_t)(ab >> 32), (uint32_t)(cd >> 32)) + (adbc >> 32) + *hi =
(adbc_carry << 32) + (uint64_t)(lo < bd); emulu(static_cast<uint32_t>(ab >> 32), static_cast<uint32_t>(cd >> 32)) +
(adbc >> 32) + (adbc_carry << 32) + static_cast<uint64_t>(lo < bd);
return lo; return lo;
} }
@ -612,7 +619,7 @@ full_multiplication(uint64_t a, uint64_t b) {
!defined(_M_ARM64) && !defined(__GNUC__)) !defined(_M_ARM64) && !defined(__GNUC__))
answer.low = _umul128(a, b, &answer.high); // _umul128 not available on ARM64 answer.low = _umul128(a, b, &answer.high); // _umul128 not available on ARM64
#elif defined(FASTFLOAT_64BIT) && defined(__SIZEOF_INT128__) #elif defined(FASTFLOAT_64BIT) && defined(__SIZEOF_INT128__)
__uint128_t r = ((__uint128_t)a) * b; __uint128_t r = static_cast<__uint128_t>(a) * b;
answer.low = uint64_t(r); answer.low = uint64_t(r);
answer.high = uint64_t(r >> 64); answer.high = uint64_t(r >> 64);
#else #else
@ -874,7 +881,7 @@ template <>
inline constexpr std::float16_t inline constexpr std::float16_t
binary_format<std::float16_t>::exact_power_of_ten(int64_t power) { binary_format<std::float16_t>::exact_power_of_ten(int64_t power) {
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)powers_of_ten[0], powers_of_ten[power]; return static_cast<void>(powers_of_ten[0]), powers_of_ten[power];
} }
template <> template <>
@ -918,7 +925,7 @@ binary_format<std::float16_t>::max_mantissa_fast_path(int64_t power) {
// power >= 0 && power <= 4 // power >= 0 && power <= 4
// //
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)max_mantissa[0], max_mantissa[power]; return static_cast<void>(max_mantissa[0]), max_mantissa[power];
} }
template <> template <>
@ -997,7 +1004,7 @@ template <>
inline constexpr std::bfloat16_t inline constexpr std::bfloat16_t
binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) { binary_format<std::bfloat16_t>::exact_power_of_ten(int64_t power) {
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)powers_of_ten[0], powers_of_ten[power]; return static_cast<void>(powers_of_ten[0]), powers_of_ten[power];
} }
template <> template <>
@ -1041,7 +1048,7 @@ binary_format<std::bfloat16_t>::max_mantissa_fast_path(int64_t power) {
// power >= 0 && power <= 3 // power >= 0 && power <= 3
// //
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)max_mantissa[0], max_mantissa[power]; return static_cast<void>(max_mantissa[0]), max_mantissa[power];
} }
template <> template <>
@ -1098,7 +1105,7 @@ binary_format<double>::max_mantissa_fast_path(int64_t power) {
// power >= 0 && power <= 22 // power >= 0 && power <= 22
// //
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)max_mantissa[0], max_mantissa[power]; return static_cast<void>(max_mantissa[0]), max_mantissa[power];
} }
template <> template <>
@ -1108,20 +1115,20 @@ binary_format<float>::max_mantissa_fast_path(int64_t power) {
// power >= 0 && power <= 10 // power >= 0 && power <= 10
// //
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)max_mantissa[0], max_mantissa[power]; return static_cast<void>(max_mantissa[0]), max_mantissa[power];
} }
template <> template <>
inline constexpr double inline constexpr double
binary_format<double>::exact_power_of_ten(int64_t power) { binary_format<double>::exact_power_of_ten(int64_t power) {
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)powers_of_ten[0], powers_of_ten[power]; return static_cast<void>(powers_of_ten[0]), powers_of_ten[power];
} }
template <> template <>
inline constexpr float binary_format<float>::exact_power_of_ten(int64_t power) { inline constexpr float binary_format<float>::exact_power_of_ten(int64_t power) {
// Work around clang bug https://godbolt.org/z/zedh7rrhc // Work around clang bug https://godbolt.org/z/zedh7rrhc
return (void)powers_of_ten[0], powers_of_ten[power]; return static_cast<void>(powers_of_ten[0]), powers_of_ten[power];
} }
template <> inline constexpr int binary_format<double>::largest_power_of_ten() { template <> inline constexpr int binary_format<double>::largest_power_of_ten() {

View File

@ -49,7 +49,7 @@ function(fast_float_add_cpp_test TEST_NAME)
target_compile_options(${TEST_NAME} PUBLIC /EHsc) target_compile_options(${TEST_NAME} PUBLIC /EHsc)
endif() endif()
if(NOT WIN32) if(NOT WIN32)
target_compile_options(${TEST_NAME} PUBLIC -Werror -Wall -Wextra -Weffc++) target_compile_options(${TEST_NAME} PUBLIC -Werror -Wall -Wextra -Weffc++ -Wold-style-cast)
target_compile_options(${TEST_NAME} PUBLIC -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wsign-conversion) target_compile_options(${TEST_NAME} PUBLIC -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wsign-conversion)
endif() endif()
target_link_libraries(${TEST_NAME} PUBLIC fast_float supplemental-data) target_link_libraries(${TEST_NAME} PUBLIC fast_float supplemental-data)

View File

@ -1065,7 +1065,7 @@ void basic_test(float val) {
do { \ do { \
constexpr int verify_comptime_var = \ constexpr int verify_comptime_var = \
(basic_test<Diag::comptime>(__VA_ARGS__), 0); \ (basic_test<Diag::comptime>(__VA_ARGS__), 0); \
(void)verify_comptime_var; \ static_cast<void>(verify_comptime_var); \
} while (false) } while (false)
#define verify_options_runtime(...) \ #define verify_options_runtime(...) \
@ -1077,7 +1077,7 @@ void basic_test(float val) {
do { \ do { \
constexpr int verify_options_comptime_var = \ constexpr int verify_options_comptime_var = \
(basic_test<Diag::comptime>(__VA_ARGS__, options), 0); \ (basic_test<Diag::comptime>(__VA_ARGS__, options), 0); \
(void)verify_options_comptime_var; \ static_cast<void>(verify_options_comptime_var); \
} while (false) } while (false)
#if defined(FASTFLOAT_CONSTEXPR_TESTS) #if defined(FASTFLOAT_CONSTEXPR_TESTS)

View File

@ -61,7 +61,7 @@ template <typename T> char *to_string(T d, char *buffer) {
} }
void strtof_from_string(char const *st, float &d) { void strtof_from_string(char const *st, float &d) {
char *pr = (char *)st; char *pr = const_cast<char *>(st);
#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \ #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
defined(sun) || defined(__sun) defined(sun) || defined(__sun)
d = cygwin_strtof_l(st, &pr); d = cygwin_strtof_l(st, &pr);

View File

@ -125,7 +125,7 @@ template <typename T> bool test() {
template <typename T> void strtod_from_string(std::string const &st, T &d); template <typename T> void strtod_from_string(std::string const &st, T &d);
template <> void strtod_from_string(std::string const &st, double &d) { template <> void strtod_from_string(std::string const &st, double &d) {
char *pr = (char *)st.c_str(); char *pr = const_cast<char *>(st.c_str());
#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \ #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
defined(sun) || defined(__sun) defined(sun) || defined(__sun)
d = cygwin_strtod_l(pr, &pr); d = cygwin_strtod_l(pr, &pr);
@ -142,7 +142,7 @@ template <> void strtod_from_string(std::string const &st, double &d) {
} }
template <> void strtod_from_string(std::string const &st, float &d) { template <> void strtod_from_string(std::string const &st, float &d) {
char *pr = (char *)st.c_str(); char *pr = const_cast<char *>(st.c_str());
#if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \ #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
defined(sun) || defined(__sun) defined(sun) || defined(__sun)
d = cygwin_strtof_l(st.c_str(), &pr); d = cygwin_strtof_l(st.c_str(), &pr);