From cb69a5fbce7149fd12d53cc94ee4bea3cf2275f4 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 29 Sep 2026 23:35:32 +0200 Subject: [PATCH] Fix CI: useless casts of the key hash of the view's object index GCC -Werror=useless-cast on Linux x86-64 rejects static_cast(key_hash(...)): the call returns a std::uint64_t prvalue, the same type as std::size_t there, while the cast is needed where std::size_t is 32 bits wide. Store the hash in a variable and cast that, which GCC does not report. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/object_index.hpp | 6 ++++-- single_include/nlohmann/json_view.hpp | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/view/object_index.hpp b/include/nlohmann/detail/view/object_index.hpp index 24c5d9ece..27348d827 100644 --- a/include/nlohmann/detail/view/object_index.hpp +++ b/include/nlohmann/detail/view/object_index.hpp @@ -71,7 +71,8 @@ inline void build_object_index(document_data& d, node* obj) for (const node* k = document_data::first_child(obj), *end = document_data::child_end(obj); k != end; k = document_data::after(k + 1)) { const char* const key = d.str(*k); - std::size_t i = static_cast(key_hash(key, k->len)) & mask; + const std::uint64_t hash = key_hash(key, k->len); // (a cast of the call would be useless where std::uint64_t is std::size_t) + std::size_t i = static_cast(hash) & mask; bool duplicate = false; while (slots[i] != 0) { @@ -107,7 +108,8 @@ inline const node* find_indexed(const document_data& d, const node* obj, const c { const document_data::object_index& ix = d.indexes[obj->extra - 1u]; const std::uint32_t* const slots = d.index_slots.data() + ix.start; - std::size_t i = static_cast(key_hash(key, n)) & ix.mask; + const std::uint64_t hash = key_hash(key, n); // (a cast of the call would be useless where std::uint64_t is std::size_t) + std::size_t i = static_cast(hash) & ix.mask; for (;;) { const std::uint32_t s = slots[i]; diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index 07c2ab5f5..64ef27ce2 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -2793,7 +2793,8 @@ inline void build_object_index(document_data& d, node* obj) for (const node* k = document_data::first_child(obj), *end = document_data::child_end(obj); k != end; k = document_data::after(k + 1)) { const char* const key = d.str(*k); - std::size_t i = static_cast(key_hash(key, k->len)) & mask; + const std::uint64_t hash = key_hash(key, k->len); // (a cast of the call would be useless where std::uint64_t is std::size_t) + std::size_t i = static_cast(hash) & mask; bool duplicate = false; while (slots[i] != 0) { @@ -2829,7 +2830,8 @@ inline const node* find_indexed(const document_data& d, const node* obj, const c { const document_data::object_index& ix = d.indexes[obj->extra - 1u]; const std::uint32_t* const slots = d.index_slots.data() + ix.start; - std::size_t i = static_cast(key_hash(key, n)) & ix.mask; + const std::uint64_t hash = key_hash(key, n); // (a cast of the call would be useless where std::uint64_t is std::size_t) + std::size_t i = static_cast(hash) & ix.mask; for (;;) { const std::uint32_t s = slots[i];