From daad8ea9b5710309a8b1013497140f16dcbdcf65 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 29 Sep 2026 00:40:11 +0200 Subject: [PATCH] Address the clang-tidy findings of element access and iteration Marks the default initializer of the item's index string (needed by GCC's -Weffc++) and, in the test, an escaped literal and a comparison of find() with end(), which is what the test is about. Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/iterator.hpp | 2 +- single_include/nlohmann/json_view.hpp | 2 +- tests/src/unit-json_view.cpp | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/nlohmann/detail/view/iterator.hpp b/include/nlohmann/detail/view/iterator.hpp index 78f148609..dfeda4a34 100644 --- a/include/nlohmann/detail/view/iterator.hpp +++ b/include/nlohmann/detail/view/iterator.hpp @@ -183,7 +183,7 @@ class view_item private: iterator m_it; - std::string m_index{}; + std::string m_index{}; // NOLINT(readability-redundant-member-init) }; /// the range returned by basic_json_view::items() diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index 88f1ff015..becfeea2e 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -1933,7 +1933,7 @@ class view_item private: iterator m_it; - std::string m_index{}; + std::string m_index{}; // NOLINT(readability-redundant-member-init) }; /// the range returned by basic_json_view::items() diff --git a/tests/src/unit-json_view.cpp b/tests/src/unit-json_view.cpp index 215e0e586..3e17f9ed1 100644 --- a/tests/src/unit-json_view.cpp +++ b/tests/src/unit-json_view.cpp @@ -505,7 +505,7 @@ TEST_CASE("json_view element access and iteration") std::vector keys = {"", "x"}; for (std::size_t n = 1; n <= 40; ++n) { - keys.push_back(std::string(n, 'k')); + keys.emplace_back(n, 'k'); keys.push_back(std::string(n, 'k') + "x"); keys.push_back("x" + std::string(n, 'k')); } @@ -513,7 +513,7 @@ TEST_CASE("json_view element access and iteration") { text += (i != 0 ? ",\"" : "\"") + keys[i] + "\":" + std::to_string(i); } - text += ",\"esc\\u0061ped\":\"escaped key\"}"; + text += ",\"esc\\u0061ped\":\"escaped key\"}"; // NOLINT(modernize-raw-string-literal) const json_document d = json_document::parse(text); const json_view root = d.root(); for (std::size_t i = 0; i < keys.size(); ++i) @@ -583,7 +583,7 @@ TEST_CASE("json_view element access and iteration") } CHECK(v.contains("a") == j.contains("a")); CHECK(v.count("a") == j.count("a")); - CHECK((v.find("a") == v.end()) == (j.find("a") == j.end())); + CHECK((v.find("a") == v.end()) == (j.find("a") == j.end())); // NOLINT(readability-container-contains): find() is what is tested } // where basic_json has undefined behavior, the view answers safely