From e9c84befa15d35339b2b7f326fcb8921c1c4c907 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 26 Sep 2026 10:07:22 +0200 Subject: [PATCH] Keep the created pointer rather than an uninitialized json_value clang-tidy reported the json_value unions declared in the string, array, and object constructors of to_json.hpp as uninitialized (cppcoreguidelines-pro-type-member-init). Assign the created pointer to the member directly once the old value is destroyed. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/conversions/to_json.hpp | 15 ++++++--------- single_include/nlohmann/json.hpp | 15 ++++++--------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index aa8d35a0f..7ba3ab71f 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -91,11 +91,10 @@ struct external_constructor int > = 0 > static void construct(BasicJsonType& j, const CompatibleStringType& str) { - typename BasicJsonType::json_value value; - value.string = j.template create(str); + auto* created = j.template create(str); j.m_data.m_value.destroy(j.m_data.m_type); j.m_data.m_type = value_t::string; - j.m_data.m_value = value; + j.m_data.m_value.string = created; j.assert_invariant(); } }; @@ -199,11 +198,10 @@ struct external_constructor using std::begin; using std::end; - typename BasicJsonType::json_value value; - value.array = j.template create(begin(arr), end(arr)); + auto* created = j.template create(begin(arr), end(arr)); j.m_data.m_value.destroy(j.m_data.m_type); j.m_data.m_type = value_t::array; - j.m_data.m_value = value; + j.m_data.m_value.array = created; j.set_parents(); j.assert_invariant(); } @@ -293,11 +291,10 @@ struct external_constructor using std::begin; using std::end; - typename BasicJsonType::json_value value; - value.object = j.template create(begin(obj), end(obj)); + auto* created = j.template create(begin(obj), end(obj)); j.m_data.m_value.destroy(j.m_data.m_type); j.m_data.m_type = value_t::object; - j.m_data.m_value = value; + j.m_data.m_value.object = created; j.set_parents(); j.assert_invariant(); } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 3f74e166f..118f1cefe 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6620,11 +6620,10 @@ struct external_constructor int > = 0 > static void construct(BasicJsonType& j, const CompatibleStringType& str) { - typename BasicJsonType::json_value value; - value.string = j.template create(str); + auto* created = j.template create(str); j.m_data.m_value.destroy(j.m_data.m_type); j.m_data.m_type = value_t::string; - j.m_data.m_value = value; + j.m_data.m_value.string = created; j.assert_invariant(); } }; @@ -6728,11 +6727,10 @@ struct external_constructor using std::begin; using std::end; - typename BasicJsonType::json_value value; - value.array = j.template create(begin(arr), end(arr)); + auto* created = j.template create(begin(arr), end(arr)); j.m_data.m_value.destroy(j.m_data.m_type); j.m_data.m_type = value_t::array; - j.m_data.m_value = value; + j.m_data.m_value.array = created; j.set_parents(); j.assert_invariant(); } @@ -6822,11 +6820,10 @@ struct external_constructor using std::begin; using std::end; - typename BasicJsonType::json_value value; - value.object = j.template create(begin(obj), end(obj)); + auto* created = j.template create(begin(obj), end(obj)); j.m_data.m_value.destroy(j.m_data.m_type); j.m_data.m_type = value_t::object; - j.m_data.m_value = value; + j.m_data.m_value.object = created; j.set_parents(); j.assert_invariant(); }