From 8b0bc70db73b48d833a3b5791e55921768cf6932 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Mon, 31 Mar 2025 08:33:08 -0500 Subject: Remove reinterpret_cast usage in favor of std::bit_cast. --- .clang-tidy | 2 +- include/fud_c_string.hpp | 21 +++------------------ include/fud_format.hpp | 6 ++---- include/fud_hash.hpp | 3 +-- include/fud_hash_map.hpp | 33 +++++++++++++++++++++++---------- include/fud_memory.hpp | 16 +++++----------- include/fud_option.hpp | 28 +++++++++++++++------------- include/fud_result.hpp | 30 ++++++++++-------------------- include/fud_string.hpp | 6 ++---- include/fud_string_view.hpp | 17 ++++++----------- include/fud_vector.hpp | 12 ++++-------- source/fud_assert.cpp | 2 +- source/fud_file.cpp | 8 +++----- source/fud_print.cpp | 3 +-- source/fud_sqlite.cpp | 3 +-- source/fud_string.cpp | 7 +++---- test/test_common.cpp | 3 +-- test/test_csv.cpp | 3 +-- test/test_file.cpp | 15 +++++---------- test/test_hash_map.cpp | 7 +++++++ test/test_string.cpp | 7 ++----- 21 files changed, 97 insertions(+), 135 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 60c819b..83853ea 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,5 @@ --- -Checks: 'clang-diagnostic-*,clang-analyzer-*,readability*,-readability-use-anyofallof,bugprone*,-bugprone-easily-swappable-parameters,deadcode,cppcoreguidelines*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-owning-memory,-cppcoreguidelines-non-private-member-variables-in-classes,modernize-*,-modernize-pass-by-value,-modernize-use-trailing-return-type,-modernize-avoid-c-arrays,performance*,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-type-union-access,-clang-diagnostic-unknown-warning-option,-clang-analyzer-valist*,-modernize-use-designated-initializers,-readability-redundant-member-init,-cppcoreguidelines-use-default-member-init,-modernize-use-default-member-init' +Checks: 'clang-diagnostic-*,clang-analyzer-*,readability*,-readability-use-anyofallof,bugprone*,-bugprone-easily-swappable-parameters,deadcode,cppcoreguidelines*,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-avoid-do-while,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-owning-memory,-cppcoreguidelines-non-private-member-variables-in-classes,modernize-*,-modernize-pass-by-value,-modernize-use-trailing-return-type,-modernize-avoid-c-arrays,performance*,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-type-union-access,-clang-diagnostic-unknown-warning-option,-clang-analyzer-valist*,-modernize-use-designated-initializers,-readability-redundant-member-init,-cppcoreguidelines-use-default-member-init,-modernize-use-default-member-init,-readability-convert-member-functions-to-static' WarningsAsErrors: '' HeaderFileExtensions: - '' diff --git a/include/fud_c_string.hpp b/include/fud_c_string.hpp index a1ab51a..0632619 100644 --- a/include/fud_c_string.hpp +++ b/include/fud_c_string.hpp @@ -18,6 +18,7 @@ #ifndef FUD_C_STRING_HPP #define FUD_C_STRING_HPP +#include #include #include #include @@ -54,28 +55,12 @@ constexpr ssize_t cStringLength(const char* str) constexpr ssize_t cStringLength(const char8_t* str, size_t maxLength) { - // Cannot cast str to const char* without breaking constexpr - // return cStringLength(reinterpret_cast(str), maxLength); - if (str == nullptr || maxLength > MAX_C_STRING_LENGTH) { - return -1; - } - - ssize_t size = 0; - - while (str[size] != 0 && static_cast(size) < maxLength) { - size++; - } - - if (str[size] != 0 && static_cast(size) == maxLength) { - return static_cast(maxLength) + 1; - } - - return size; + return cStringLength(std::bit_cast(str), maxLength); } constexpr ssize_t cStringLength(const char8_t* str) { - return cStringLength(str, MAX_C_STRING_LENGTH); + return cStringLength(std::bit_cast(str), MAX_C_STRING_LENGTH); } } // namespace fud diff --git a/include/fud_format.hpp b/include/fud_format.hpp index 0dff3c1..4536d69 100644 --- a/include/fud_format.hpp +++ b/include/fud_format.hpp @@ -1396,8 +1396,7 @@ FormatResult format(Sink& sink, FormatCharMode formatMode, const FormatSpec& for template FormatResult format(Sink& sink, FormatCharMode formatMode, const FormatSpec& formatSpec, const utf8* arg) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return format(sink, formatMode, formatSpec, reinterpret_cast(arg)); + return format(sink, formatMode, formatSpec, std::bit_cast(arg)); } template @@ -1493,8 +1492,7 @@ FormatResult format(Sink& sink, FormatCharMode formatMode, const FormatSpec& for return DrainResult{0, FudStatus::FormatInvalid}; } pointerFormatSpec.alternateForm = true; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return impl::formatIntegral(sink, formatMode, pointerFormatSpec, reinterpret_cast(arg)); + return impl::formatIntegral(sink, formatMode, pointerFormatSpec, std::bit_cast(arg)); } template diff --git a/include/fud_hash.hpp b/include/fud_hash.hpp index 57b5619..a472171 100644 --- a/include/fud_hash.hpp +++ b/include/fud_hash.hpp @@ -57,8 +57,7 @@ struct DefaultHash { size_t operator()(const T& value, size_t seed) const { static_cast(seed); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return djb2(reinterpret_cast(&value), sizeof(value)); + return djb2(std::bit_cast(&value), sizeof(value)); } }; diff --git a/include/fud_hash_map.hpp b/include/fud_hash_map.hpp index 4333df7..7980bf4 100644 --- a/include/fud_hash_map.hpp +++ b/include/fud_hash_map.hpp @@ -156,8 +156,25 @@ class HashMap { Option extractPair(const Key& key); Option extractPair(Key& key); - Option get(const Key& key) const; - Option getRef(const Key& key); + Option get(const Key& key) + { + auto hashIndexOption = lookup(key); + if (hashIndexOption.isNone()) { + return NullOpt; + } + + return m_data[hashIndexOption.value()].value().m_value; + } + + Option getRef(const Key& key) const + { + auto hashIndexOption = lookup(key); + if (hashIndexOption.isNone()) { + return NullOpt; + } + + return m_data[hashIndexOption.value()].value().m_value; + } Option getConstRef(const Key& key) const { @@ -206,8 +223,7 @@ class HashMap { return dataPtrResult.takeError(); } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto* dataPtr = reinterpret_cast(dataPtrResult.takeOkay()); + auto* dataPtr = std::bit_cast(dataPtrResult.takeOkay()); for (size_t index = 0; index < count; ++index) { const auto* ptr = new (dataPtr + index) Node(NullOpt); fudAssert(ptr != nullptr); @@ -219,8 +235,7 @@ class HashMap { const auto hash = m_hasher(key, m_seed); auto newHashIndexResult = findEmptyBucket(key, count, dataPtr); if (newHashIndexResult.isError()) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_allocator->deallocate(reinterpret_cast(dataPtr), requestedSize); + m_allocator->deallocate(std::bit_cast(dataPtr), requestedSize); return FudStatus::Failure; } const auto newHashIndex{newHashIndexResult.takeOkay()}; @@ -233,8 +248,7 @@ class HashMap { auto status = FudStatus::Success; if (m_buckets > 0) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_allocator->deallocate(reinterpret_cast(m_data), m_buckets * NodeSize); + m_allocator->deallocate(std::bit_cast(m_data), m_buckets * NodeSize); } m_data = dataPtr; @@ -295,8 +309,7 @@ class HashMap { { auto status = clear(); if (m_data != nullptr && m_allocator != nullptr) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_allocator->deallocate(reinterpret_cast(m_data), m_buckets); + m_allocator->deallocate(std::bit_cast(m_data), m_buckets); } m_allocator = nullptr; diff --git a/include/fud_memory.hpp b/include/fud_memory.hpp index 6f6816f..bb60ab7 100644 --- a/include/fud_memory.hpp +++ b/include/fud_memory.hpp @@ -79,7 +79,7 @@ constexpr void zeroObject(T& object) static_assert(std::is_standard_layout_v); static_assert(std::is_trivially_copyable_v); - auto* objPtr = reinterpret_cast(&object); + auto* objPtr = std::bit_cast(&object); for (size_t idx = 0; idx < sizeof(object); ++idx) { objPtr[idx] = 0; } @@ -95,10 +95,8 @@ void copyMem(T& destination, const U& source) static_assert(std::is_trivially_copyable_v); static_assert(std::is_trivially_copyable_v); - // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) - auto* destPtr = reinterpret_cast(&destination); - const auto* srcPtr = reinterpret_cast(&source); - // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) + auto* destPtr = std::bit_cast(&destination); + const auto* srcPtr = std::bit_cast(&source); for (size_t idx = 0; idx < Count; ++idx) { destPtr[idx] = srcPtr[idx]; @@ -121,9 +119,7 @@ int compareMem(const T& lhs, const U& rhs) int difference = 0; for (size_t idx = 0; idx < Count; ++idx) { - // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) - difference = reinterpret_cast(&lhs)[idx] - reinterpret_cast(&rhs)[idx]; - // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) + difference = std::bit_cast(&lhs)[idx] - std::bit_cast(&rhs)[idx]; if (difference != 0) { break; } @@ -142,9 +138,7 @@ int compareMem(const T& lhs, U&& rhs) int difference = 0; for (size_t idx = 0; idx < Count; ++idx) { - // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) - difference = reinterpret_cast(&lhs)[idx] - reinterpret_cast(&uRhs)[idx]; - // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) + difference = std::bit_cast(&lhs)[idx] - std::bit_cast(&uRhs)[idx]; if (difference != 0) { break; } diff --git a/include/fud_option.hpp b/include/fud_option.hpp index 5a5611f..f2e86ca 100644 --- a/include/fud_option.hpp +++ b/include/fud_option.hpp @@ -172,11 +172,9 @@ class Option { { fudAssert(m_engaged); if constexpr (IsRef) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return *reinterpret_cast*>(m_data.data()); + return *std::bit_cast*>(m_data.data()); } else { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return *reinterpret_cast(m_data.data()); + return *std::bit_cast(m_data.data()); } } @@ -184,11 +182,9 @@ class Option { { fudAssert(m_engaged); if constexpr (IsRef) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return *reinterpret_cast*>(m_data.data()); + return *std::bit_cast*>(m_data.data()); } else { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return *reinterpret_cast(m_data.data()); + return *std::bit_cast(m_data.data()); } } @@ -196,8 +192,15 @@ class Option { { fudAssert(m_engaged); static_assert(!IsRef); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::move(*reinterpret_cast(m_data.data())); + return std::move(*std::bit_cast(m_data.data())); + } + + [[nodiscard]] ValueType& mutValueOr(ValueType& alternative) + { + if (m_engaged) { + return value(); + } + return alternative; } [[nodiscard]] constexpr const ValueType& valueOr(const ValueType& alternative) const& @@ -232,10 +235,9 @@ class Option { { if (m_engaged) { if constexpr (IsRef) { - // reinterpret_cast*>(m_data.data()); + // do nothing } else { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast(m_data.data())->~ValueType(); + std::bit_cast(m_data.data())->~ValueType(); } cleanup(); } diff --git a/include/fud_result.hpp b/include/fud_result.hpp index 88d0dc4..0394156 100644 --- a/include/fud_result.hpp +++ b/include/fud_result.hpp @@ -200,8 +200,7 @@ class [[nodiscard]] Result { [[nodiscard]] constexpr const T& getOkay() const& { fudAssert(isOkay()); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return *reinterpret_cast(m_data.data()); + return *std::bit_cast(m_data.data()); } [[nodiscard]] constexpr const T& getOkayOr(const T& alternative) const& @@ -210,15 +209,13 @@ class [[nodiscard]] Result { // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter) return alternative; } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return *reinterpret_cast(m_data.data()); + return *std::bit_cast(m_data.data()); } [[nodiscard]] constexpr const E& getError() const& { fudAssert(isError()); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return *reinterpret_cast(m_data.data()); + return *std::bit_cast(m_data.data()); } [[nodiscard]] constexpr const E& getErrorOr(const E& alternative) const& @@ -227,15 +224,13 @@ class [[nodiscard]] Result { // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter) return alternative; } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return *reinterpret_cast(m_data.data()); + return *std::bit_cast(m_data.data()); } [[nodiscard]] constexpr T&& takeOkay() { fudAssert(isOkay()); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::move(*reinterpret_cast(m_data.data())); + return std::move(*std::bit_cast(m_data.data())); } [[nodiscard]] constexpr T&& takeOkayOr(T&& alternative) @@ -243,15 +238,13 @@ class [[nodiscard]] Result { if (!isOkay()) { return std::move(alternative); } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::move(*reinterpret_cast(m_data.data())); + return std::move(*std::bit_cast(m_data.data())); } [[nodiscard]] constexpr E&& takeError() { fudAssert(isError()); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::move(*reinterpret_cast(m_data.data())); + return std::move(*std::bit_cast(m_data.data())); } [[nodiscard]] constexpr E&& takeErrorOr(E&& alternative) @@ -259,8 +252,7 @@ class [[nodiscard]] Result { if (!isError()) { return std::move(alternative); } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::move(*reinterpret_cast(m_data.data())); + return std::move(*std::bit_cast(m_data.data())); } private: @@ -307,12 +299,10 @@ class [[nodiscard]] Result { constexpr void destroy() noexcept { if (m_discriminant == Discriminant::Okay) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast(m_data.data())->~T(); + std::bit_cast(m_data.data())->~T(); m_discriminant = Discriminant::Invalid; } else if (m_discriminant == Discriminant::Error) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast(m_data.data())->~E(); + std::bit_cast(m_data.data())->~E(); m_discriminant = Discriminant::Invalid; } } diff --git a/include/fud_string.hpp b/include/fud_string.hpp index 1c189c2..2c2173b 100644 --- a/include/fud_string.hpp +++ b/include/fud_string.hpp @@ -133,8 +133,7 @@ class String { if constexpr (std::is_same_v) { cString = cStringItem; } else if constexpr (std::is_same_v) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - cString = reinterpret_cast(cStringItem); + cString = std::bit_cast(cStringItem); } else { static_assert(!std::is_same_v); } @@ -275,8 +274,7 @@ class String { /** \brief The underlying data as an explicit c string. */ [[nodiscard]] const char* c_str() const { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return reinterpret_cast(data()); + return std::bit_cast(data()); } [[nodiscard]] StringView asView() const diff --git a/include/fud_string_view.hpp b/include/fud_string_view.hpp index f71919d..e3ff4d7 100644 --- a/include/fud_string_view.hpp +++ b/include/fud_string_view.hpp @@ -56,16 +56,14 @@ struct StringView { // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) StringView(const char (&input)[N]) : m_length{N - 1}, - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_data{reinterpret_cast(input)} + m_data{std::bit_cast(&input[0])} { static_assert(N > 0); } StringView(size_t strLen, const char* strData) : m_length(strLen), - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_data{reinterpret_cast(strData)} + m_data{std::bit_cast(&strData[0])} { } @@ -80,13 +78,11 @@ struct StringView { constexpr static StringView makeFromCString(const char (&input)[N]) { static_assert(N > 0); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return StringView{N - 1, reinterpret_cast(input)}; + return StringView{N - 1, std::bit_cast(&input[0])}; } - [[nodiscard]] std::string_view as_string_view() const { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return std::string_view{reinterpret_cast(m_data), m_length}; + [[nodiscard]] constexpr std::string_view as_string_view() const { + return std::string_view{std::bit_cast(m_data), m_length}; } [[nodiscard]] constexpr Span asSpan() const @@ -106,8 +102,7 @@ struct StringView { [[nodiscard]] const char* c_str() const { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return reinterpret_cast(m_data); + return std::bit_cast(m_data); } constexpr const utf8& operator[](size_t index) const diff --git a/include/fud_vector.hpp b/include/fud_vector.hpp index 9808a7d..1ba2abf 100644 --- a/include/fud_vector.hpp +++ b/include/fud_vector.hpp @@ -124,8 +124,7 @@ class Vector { } output.m_allocator = allocator; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - output.m_data = reinterpret_cast(dataPtrResult.getOkay()); + output.m_data = std::bit_cast(dataPtrResult.getOkay()); output.m_length = 0; output.m_capacity = capacity; return FudStatus::Success; @@ -365,8 +364,7 @@ class Vector { return dataPtrResult.takeError(); } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto* dataPtr = reinterpret_cast(dataPtrResult.takeOkay()); + auto* dataPtr = std::bit_cast(dataPtrResult.takeOkay()); for (size_t index = 0; index < m_length; ++index) { const auto* ptr = new (dataPtr + index) T(std::move(m_data[index])); fudAssert(ptr != nullptr); @@ -375,8 +373,7 @@ class Vector { auto status = FudStatus::Success; if (m_capacity > 0) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_allocator->deallocate(reinterpret_cast(m_data), m_capacity * ElementSize); + m_allocator->deallocate(std::bit_cast(m_data), m_capacity * ElementSize); } m_data = dataPtr; @@ -748,8 +745,7 @@ class Vector { auto status = clear(); if (m_data != nullptr && m_allocator != nullptr) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_allocator->deallocate(reinterpret_cast(m_data), m_capacity * ElementSize); + m_allocator->deallocate(std::bit_cast(m_data), m_capacity * ElementSize); } m_allocator = nullptr; diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp index 425826d..64b3275 100644 --- a/source/fud_assert.cpp +++ b/source/fud_assert.cpp @@ -42,7 +42,7 @@ DrainResult StdErrSink::drain(StringView source) return result; } /* TODO: give users control over this functionality */ - result.bytesDrained = fwrite(reinterpret_cast(source.m_data), 1, source.m_length, stderr); + result.bytesDrained = fwrite(std::bit_cast(source.m_data), 1, source.m_length, stderr); if (result.bytesDrained != source.m_length) { result.status = FudStatus::Full; } diff --git a/source/fud_file.cpp b/source/fud_file.cpp index 7219638..9666b14 100644 --- a/source/fud_file.cpp +++ b/source/fud_file.cpp @@ -728,8 +728,7 @@ DrainResult BufferedRegularFile::readUtf8(Utf8& sink, Option maxExtraAtt { size_t extraAttempts{maxExtraAttempts.valueOr(0)}; Array utf8Data{}; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto drainResult = read(reinterpret_cast(utf8Data.data()), 1, maxExtraAttempts); + auto drainResult = read(std::bit_cast(utf8Data.data()), 1, maxExtraAttempts); if (drainResult.status != FudStatus::Success) { return drainResult; } @@ -756,8 +755,7 @@ DrainResult BufferedRegularFile::readUtf8(Utf8& sink, Option maxExtraAtt } if (bytesToRead > 0) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto utf8ReadResult = read(reinterpret_cast(utf8Data.data() + 1), bytesToRead, extraAttempts); + auto utf8ReadResult = read(std::bit_cast(utf8Data.data() + 1), bytesToRead, extraAttempts); drainResult.status = utf8ReadResult.status; drainResult.bytesDrained += utf8ReadResult.bytesDrained; } @@ -784,7 +782,7 @@ void BufferedRegularFile::drainReadBuffer(std::byte*& sink, size_t& length, Drai } } -// NOLINTNEXTLINE(readability-convert-member-functions-to-static,cppcoreguidelines-rvalue-*) +// NOLINTNEXTLINE(cppcoreguidelines-rvalue-*) FudStatus BufferedRegularFile::setBuffer(Vector&& buffer, bool discardOldBuffer) { static_cast(buffer); diff --git a/source/fud_print.cpp b/source/fud_print.cpp index 85a3d76..6b7bac1 100644 --- a/source/fud_print.cpp +++ b/source/fud_print.cpp @@ -20,7 +20,6 @@ #include "fud_string_view.hpp" #include -#include namespace fud { @@ -36,7 +35,7 @@ DrainResult StdOutSink::drain(StringView source) return result; } /* TODO: give users control over this functionality */ - result.bytesDrained = fwrite(reinterpret_cast(source.m_data), 1, source.m_length, stdout); + result.bytesDrained = fwrite(std::bit_cast(source.m_data), 1, source.m_length, stdout); if (result.bytesDrained != source.m_length) { result.status = FudStatus::Full; } diff --git a/source/fud_sqlite.cpp b/source/fud_sqlite.cpp index fccbc1e..c607b37 100644 --- a/source/fud_sqlite.cpp +++ b/source/fud_sqlite.cpp @@ -264,8 +264,7 @@ Result SqliteDb::columnText(SqliteStatement& statement, i } output.m_length = static_cast(outputLength); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - output.m_data = reinterpret_cast(sqlite3_column_text(statement.statement(), index)); + output.m_data = std::bit_cast(sqlite3_column_text(statement.statement(), index)); if (output.m_data == nullptr && output.m_length != 0) { return Error{FudStatus::Failure}; } diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 777212d..d4fd691 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -241,9 +241,9 @@ String& String::operator=(String&& rhs) noexcept void String::cleanup() { - const auto* allocPtr = allocator(); + auto* allocPtr = allocator(); if (isLarge() && m_repr.large.data != nullptr && allocPtr != nullptr) { - allocator()->deallocate(std::bit_cast(m_repr.large.data), m_repr.large.capacity); + allocPtr->deallocate(std::bit_cast(m_repr.large.data), m_repr.large.capacity); m_repr.large.data = nullptr; } } @@ -816,8 +816,7 @@ FudStatus String::makeLarge(size_t cap, size_t len, utf8*& outputData) if (dataResult.isError()) { return dataResult.getError(); } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - m_repr.large.data = reinterpret_cast(dataResult.getOkay()); + m_repr.large.data = std::bit_cast(dataResult.getOkay()); outputData = m_repr.large.data; setLarge(); return FudStatus::Success; diff --git a/test/test_common.cpp b/test/test_common.cpp index 03c5dff..926bd49 100644 --- a/test/test_common.cpp +++ b/test/test_common.cpp @@ -105,8 +105,7 @@ FudStatus removeRecursive(StringView path) } constexpr int maxOpenFd = 64; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto status = nftw(reinterpret_cast(path.data()), unlink_cb, maxOpenFd, FTW_DEPTH | FTW_PHYS); + auto status = nftw(std::bit_cast(path.data()), unlink_cb, maxOpenFd, FTW_DEPTH | FTW_PHYS); if (status == 0) { return FudStatus::Success; } diff --git a/test/test_csv.cpp b/test/test_csv.cpp index 83bfe0a..95b0aec 100644 --- a/test/test_csv.cpp +++ b/test/test_csv.cpp @@ -79,8 +79,7 @@ auto writeHappyCsv() -> FudStatus } auto file{fileResult.takeOkay()}; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto writeResult = file.write(reinterpret_cast(happyData.data()), happyData.length()); + auto writeResult = file.write(std::bit_cast(happyData.data()), happyData.length()); if (writeResult.status != FudStatus::Success) { return writeResult.status; } diff --git a/test/test_file.cpp b/test/test_file.cpp index 878c740..17469e1 100644 --- a/test/test_file.cpp +++ b/test/test_file.cpp @@ -119,8 +119,7 @@ TEST(FudBufferedFile, OpenReadWrite) ASSERT_EQ(bufferedFile.file().size().getOkayOr(std::numeric_limits::max()), 0); auto writeResult = bufferedFile.write( - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast(testName.data()), + std::bit_cast(testName.data()), testName.size(), NullOpt); DrainResult expected{testName.size(), FudStatus::Success}; @@ -131,8 +130,7 @@ TEST(FudBufferedFile, OpenReadWrite) ASSERT_EQ(bufferedFile.seekStart(), FudStatus::Success); Vector output{Vector::withSize(testName.size()).takeOkay()}; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto readResult = bufferedFile.read(reinterpret_cast(output.data()), testName.size(), NullOpt); + auto readResult = bufferedFile.read(std::bit_cast(output.data()), testName.size(), NullOpt); ASSERT_EQ(readResult, expected); EXPECT_EQ(output.size(), testName.size()); @@ -142,22 +140,19 @@ TEST(FudBufferedFile, OpenReadWrite) ASSERT_EQ(bufferedFile.seekStart(), FudStatus::Success); expected.bytesDrained = 1; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - readResult = bufferedFile.read(reinterpret_cast(output.data()), 1, NullOpt); + readResult = bufferedFile.read(std::bit_cast(output.data()), 1, NullOpt); ASSERT_EQ(readResult, expected); EXPECT_EQ(output[0], testName.data()[0]); expected.bytesDrained = testName.size() - 2; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - readResult = bufferedFile.read(reinterpret_cast(output.data()) + 1, testName.size() - 2, NullOpt); + readResult = bufferedFile.read(std::bit_cast(output.data()) + 1, testName.size() - 2, NullOpt); ASSERT_EQ(readResult, expected); EXPECT_EQ( 0, compareMem(output.data() + 1, output.size() - 1, testName.data() + 1, testName.size() - 2).takeOkayOr(-1)); expected.bytesDrained = 1; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - readResult = bufferedFile.read(reinterpret_cast(output.data()), 1, NullOpt); + readResult = bufferedFile.read(std::bit_cast(output.data()), 1, NullOpt); EXPECT_TRUE(readResult.status == FudStatus::Success || readResult.status == FudStatus::Partial); EXPECT_EQ(output[testName.size() - 1], testName.data()[testName.size() - 1]); diff --git a/test/test_hash_map.cpp b/test/test_hash_map.cpp index aa0ba49..065ae81 100644 --- a/test/test_hash_map.cpp +++ b/test/test_hash_map.cpp @@ -87,6 +87,13 @@ TEST(FudHash, InsertMoveKeyCopyValue) const int invalid = -1; EXPECT_EQ(mapString2Int.getConstRef(stringList[index]).valueOr(invalid), index); } + + for (int index = 0; index < static_cast(stringList.size()); ++index) { + int invalid = -1; + int& refVal = mapString2Int.getRef(stringList[index]).mutValueOr(invalid); + refVal *= 2; + EXPECT_EQ(mapString2Int.getConstRef(stringList[index]).valueOr(invalid), index * 2); + } } TEST(FudHash, InsertCopyKeyMoveValue) diff --git a/test/test_string.cpp b/test/test_string.cpp index 39a24cb..f63ed71 100644 --- a/test/test_string.cpp +++ b/test/test_string.cpp @@ -39,8 +39,7 @@ TEST(FudString, BasicStringOps) ASSERT_FALSE(Ascii::valid(invalid[0])); const Array invalid2{0xFF, 0x00}; - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto stringResult = String::makeFromCString(reinterpret_cast(invalid2.data())); + auto stringResult = String::makeFromCString(std::bit_cast(invalid2.data())); ASSERT_TRUE(stringResult.isOkay()); String fudString{stringResult.takeOkay()}; @@ -125,11 +124,9 @@ TEST(FudString, FindSubstringCxx) FudStringView stringView{}; auto findStatus = ext_string_find_substring(haystack, needle, &stringView); ASSERT_EQ(findStatus, ExtSuccess); - // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) ASSERT_EQ( ext_string_get_c_string(&extString) + sizeof("why waste time"), - reinterpret_cast(stringView.data)); - // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) + std::bit_cast(stringView.data)); } TEST(TestFudString, StringBuffer) -- cgit v1.2.3