Fix the clang-tidy findings in the diff and JSON pointer code

- diff_target_values holds its inline values in a std::array (a type
  with an empty user-provided constructor, as the serializer's buffers,
  so that -Weffc++ is met and they stay uninitialized) and declares its
  move members as deleted
- auto for an iterator and a cast, and parentheses around a %

json::diff() instructions are unchanged within noise (Apple clang,
twitter and citm_catalog).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-22 17:45:18 +02:00
1 parent 892a2dd957
commit d96ec00d53
3 files changed
+38 -12

No files matched your search

+2 -2
View File
@@ -874,7 +874,7 @@ class json_pointer
// an index of up to 18 digits without a leading zero fits into size_type // an index of up to 18 digits without a leading zero fits into size_type
// (9 digits for a 32-bit size_type); everything else is left to json_pointer // (9 digits for a 32-bit size_type); everything else is left to json_pointer
constexpr std::size_t max_digits = sizeof(typename BasicJsonType::size_type) >= 8 ? 18 : 9; constexpr std::size_t max_digits = sizeof(typename BasicJsonType::size_type) >= 8 ? 18 : 9;
const std::size_t digits = static_cast<std::size_t>(end - begin); const auto digits = static_cast<std::size_t>(end - begin);
if (JSON_HEDLEY_UNLIKELY(digits == 0 || digits > max_digits || (digits > 1 && *begin == '0'))) if (JSON_HEDLEY_UNLIKELY(digits == 0 || digits > max_digits || (digits > 1 && *begin == '0')))
{ {
return nullptr; return nullptr;
@@ -1575,7 +1575,7 @@ class json_pointer
std::size_t rest = i; std::size_t rest = i;
do do
{ {
digits[--pos] = static_cast<char>('0' + rest % 10); digits[--pos] = static_cast<char>('0' + (rest % 10));
rest /= 10; rest /= 10;
} }
while (rest != 0); while (rest != 0);
+17 -4
View File
@@ -19,6 +19,7 @@
#define INCLUDE_NLOHMANN_JSON_HPP_ #define INCLUDE_NLOHMANN_JSON_HPP_
#include <algorithm> // all_of, find, for_each, none_of #include <algorithm> // all_of, find, for_each, none_of
#include <array> // array
#include <cmath> // isnan #include <cmath> // isnan
#include <cstddef> // nullptr_t, ptrdiff_t, size_t #include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstdint> // uint8_t #include <cstdint> // uint8_t
@@ -7447,7 +7448,7 @@ public:
// added without looking them up // added without looking them up
basic_json entry(value_t::object); basic_json entry(value_t::object);
auto& members = *entry.m_data.m_value.object; auto& members = *entry.m_data.m_value.object;
typename object_t::iterator hint = members.end(); auto hint = members.end();
json_pointer::add_flattened(members, hint, string_t("op"), string_t(op), detail::priority_tag<2> {}); json_pointer::add_flattened(members, hint, string_t("op"), string_t(op), detail::priority_tag<2> {});
json_pointer::add_flattened(members, hint, string_t("path"), path, detail::priority_tag<2> {}); json_pointer::add_flattened(members, hint, string_t("path"), path, detail::priority_tag<2> {});
if (value != nullptr) if (value != nullptr)
@@ -7483,9 +7484,13 @@ public:
class diff_target_values class diff_target_values
{ {
public: public:
diff_target_values() = default; diff_target_values() noexcept
: inline_items() // NOLINT(readability-redundant-member-init)
{}
diff_target_values(const diff_target_values&) = delete; diff_target_values(const diff_target_values&) = delete;
diff_target_values& operator=(const diff_target_values&) = delete; diff_target_values& operator=(const diff_target_values&) = delete;
diff_target_values(diff_target_values&&) = delete;
diff_target_values& operator=(diff_target_values&&) = delete;
~diff_target_values() = default; ~diff_target_values() = default;
void reserve(const std::size_t n) void reserve(const std::size_t n)
@@ -7514,10 +7519,18 @@ public:
private: private:
static constexpr std::size_t inline_size = 16; static constexpr std::size_t inline_size = 16;
/// the values of a small object, left uninitialized: they are written
/// before they are read (the user-provided constructor keeps the
/// member initialization, which -Weffc++ asks for, from zeroing them)
struct uninitialized_items : std::array<const basic_json*, inline_size>
{
uninitialized_items() noexcept {} // NOLINT(modernize-use-equals-default,hicpp-use-equals-default,cppcoreguidelines-pro-type-member-init,hicpp-member-init): see above
};
std::size_t count = 0; std::size_t count = 0;
const basic_json** items = inline_items;
std::vector<const basic_json*> large{}; // NOLINT(readability-redundant-member-init) std::vector<const basic_json*> large{}; // NOLINT(readability-redundant-member-init)
const basic_json* inline_items[inline_size]; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) uninitialized_items inline_items;
const basic_json** items = inline_items.data();
}; };
/// @brief append "/" and the decimal representation of @a index to @a path /// @brief append "/" and the decimal representation of @a index to @a path
+19 -6
View File
@@ -19,6 +19,7 @@
#define INCLUDE_NLOHMANN_JSON_HPP_ #define INCLUDE_NLOHMANN_JSON_HPP_
#include <algorithm> // all_of, find, for_each, none_of #include <algorithm> // all_of, find, for_each, none_of
#include <array> // array
#include <cmath> // isnan #include <cmath> // isnan
#include <cstddef> // nullptr_t, ptrdiff_t, size_t #include <cstddef> // nullptr_t, ptrdiff_t, size_t
#include <cstdint> // uint8_t #include <cstdint> // uint8_t
@@ -23224,7 +23225,7 @@ class json_pointer
// an index of up to 18 digits without a leading zero fits into size_type // an index of up to 18 digits without a leading zero fits into size_type
// (9 digits for a 32-bit size_type); everything else is left to json_pointer // (9 digits for a 32-bit size_type); everything else is left to json_pointer
constexpr std::size_t max_digits = sizeof(typename BasicJsonType::size_type) >= 8 ? 18 : 9; constexpr std::size_t max_digits = sizeof(typename BasicJsonType::size_type) >= 8 ? 18 : 9;
const std::size_t digits = static_cast<std::size_t>(end - begin); const auto digits = static_cast<std::size_t>(end - begin);
if (JSON_HEDLEY_UNLIKELY(digits == 0 || digits > max_digits || (digits > 1 && *begin == '0'))) if (JSON_HEDLEY_UNLIKELY(digits == 0 || digits > max_digits || (digits > 1 && *begin == '0')))
{ {
return nullptr; return nullptr;
@@ -23925,7 +23926,7 @@ class json_pointer
std::size_t rest = i; std::size_t rest = i;
do do
{ {
digits[--pos] = static_cast<char>('0' + rest % 10); digits[--pos] = static_cast<char>('0' + (rest % 10));
rest /= 10; rest /= 10;
} }
while (rest != 0); while (rest != 0);
@@ -39956,7 +39957,7 @@ public:
// added without looking them up // added without looking them up
basic_json entry(value_t::object); basic_json entry(value_t::object);
auto& members = *entry.m_data.m_value.object; auto& members = *entry.m_data.m_value.object;
typename object_t::iterator hint = members.end(); auto hint = members.end();
json_pointer::add_flattened(members, hint, string_t("op"), string_t(op), detail::priority_tag<2> {}); json_pointer::add_flattened(members, hint, string_t("op"), string_t(op), detail::priority_tag<2> {});
json_pointer::add_flattened(members, hint, string_t("path"), path, detail::priority_tag<2> {}); json_pointer::add_flattened(members, hint, string_t("path"), path, detail::priority_tag<2> {});
if (value != nullptr) if (value != nullptr)
@@ -39992,9 +39993,13 @@ public:
class diff_target_values class diff_target_values
{ {
public: public:
diff_target_values() = default; diff_target_values() noexcept
: inline_items() // NOLINT(readability-redundant-member-init)
{}
diff_target_values(const diff_target_values&) = delete; diff_target_values(const diff_target_values&) = delete;
diff_target_values& operator=(const diff_target_values&) = delete; diff_target_values& operator=(const diff_target_values&) = delete;
diff_target_values(diff_target_values&&) = delete;
diff_target_values& operator=(diff_target_values&&) = delete;
~diff_target_values() = default; ~diff_target_values() = default;
void reserve(const std::size_t n) void reserve(const std::size_t n)
@@ -40023,10 +40028,18 @@ public:
private: private:
static constexpr std::size_t inline_size = 16; static constexpr std::size_t inline_size = 16;
/// the values of a small object, left uninitialized: they are written
/// before they are read (the user-provided constructor keeps the
/// member initialization, which -Weffc++ asks for, from zeroing them)
struct uninitialized_items : std::array<const basic_json*, inline_size>
{
uninitialized_items() noexcept {} // NOLINT(modernize-use-equals-default,hicpp-use-equals-default,cppcoreguidelines-pro-type-member-init,hicpp-member-init): see above
};
std::size_t count = 0; std::size_t count = 0;
const basic_json** items = inline_items;
std::vector<const basic_json*> large{}; // NOLINT(readability-redundant-member-init) std::vector<const basic_json*> large{}; // NOLINT(readability-redundant-member-init)
const basic_json* inline_items[inline_size]; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) uninitialized_items inline_items;
const basic_json** items = inline_items.data();
}; };
/// @brief append "/" and the decimal representation of @a index to @a path /// @brief append "/" and the decimal representation of @a index to @a path