From 1fa7c9a8e4d9cee6b29a5bf666bdd007f871d83a Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 24 Sep 2026 07:20:18 +0200 Subject: [PATCH] Fix the cppcheck, clang-tidy and legacy-comparison CI failures - to_json_tuple_impl() moved the element in both branches of a ternary; only one runs, but cppcheck reported accessMoved. Use if/else. - The ABI tag test looked for "json_abi_bics", which misses when another tag comes first, as in json_abi_ldvcmp_bics; look for "_bics". - readability-qualified-auto in the items() test. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/conversions/to_json.hpp | 9 ++++++++- single_include/nlohmann/json.hpp | 9 ++++++++- tests/src/unit-brace-init-copy-semantics.cpp | 5 +++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index dc7fd4007..491bb9873 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -484,7 +484,14 @@ inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence< // keeps a string type constructible from 0 from selecting operator[](key) const bool is_member = element.is_array() && element.size() == 2 && element[static_cast(0)].is_string(); - j = is_member ? BasicJsonType::object({std::move(element)}) : BasicJsonType::array({std::move(element)}); + if (is_member) + { + j = BasicJsonType::object({std::move(element)}); + } + else + { + j = BasicJsonType::array({std::move(element)}); + } } #endif diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f6692da11..c23d8b89f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6778,7 +6778,14 @@ inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence< // keeps a string type constructible from 0 from selecting operator[](key) const bool is_member = element.is_array() && element.size() == 2 && element[static_cast(0)].is_string(); - j = is_member ? BasicJsonType::object({std::move(element)}) : BasicJsonType::array({std::move(element)}); + if (is_member) + { + j = BasicJsonType::object({std::move(element)}); + } + else + { + j = BasicJsonType::array({std::move(element)}); + } } #endif diff --git a/tests/src/unit-brace-init-copy-semantics.cpp b/tests/src/unit-brace-init-copy-semantics.cpp index 63631de6e..1ee0c6607 100644 --- a/tests/src/unit-brace-init-copy-semantics.cpp +++ b/tests/src/unit-brace-init-copy-semantics.cpp @@ -35,7 +35,8 @@ TEST_CASE("JSON_BRACE_INIT_COPY_SEMANTICS") SECTION("the macro is part of the ABI tag") { const std::string ns = STRINGIZE(NLOHMANN_JSON_NAMESPACE); - CHECK(ns.find("json_abi_bics") != std::string::npos); + // other tags may come before it, e.g. json_abi_ldvcmp_bics + CHECK(ns.find("_bics") != std::string::npos); } SECTION("single-element brace initialization copies the element (#5074)") @@ -156,7 +157,7 @@ TEST_CASE("JSON_BRACE_INIT_COPY_SEMANTICS") SECTION("items()") { json j_obj = {{"key", 1}}; - for (auto& el : j_obj.items()) + for (const auto& el : j_obj.items()) { json const j = el; CHECK(j.dump() == "{\"key\":1}");