From 2cf9df3eae2dfaba3182804f0ec10b252fafb789 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 22 Oct 2026 19:46:31 +0200 Subject: [PATCH] Fix the clang-tidy findings in json_view's dump and its tests - the SSE2 loop of the string scan is within NOLINTBEGIN/END (portability-simd-intrinsics), as in to_chars.hpp - depth_counter's leave() and get() keep the interface of depth_counter instead of being static - write_ascii() compares the room left as a size_t variable - the test's texts are a std::array, and a JSON fragment with escapes is a raw string literal Signed-off-by: Niels Lohmann --- include/nlohmann/detail/view/serializer.hpp | 9 ++++++--- single_include/nlohmann/json_view.hpp | 9 ++++++--- tests/src/unit-json_view_dump.cpp | 12 +++++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/include/nlohmann/detail/view/serializer.hpp b/include/nlohmann/detail/view/serializer.hpp index 01d9286c6..a47dee182 100644 --- a/include/nlohmann/detail/view/serializer.hpp +++ b/include/nlohmann/detail/view/serializer.hpp @@ -141,6 +141,7 @@ inline std::size_t plain_output_run(const unsigned char* s, std::size_t n) noexc #if NLOHMANN_VIEW_SSE2 // (16 bytes per step; with NEON, the mask of a step costs more than it saves on the // short runs that are typical here) + // NOLINTBEGIN(portability-simd-intrinsics) for (; i + 16 <= n; i += 16) { const __m128i in = _mm_loadu_si128(static_cast(static_cast(s + i))); // NOLINT(bugprone-casting-through-void) @@ -153,6 +154,7 @@ inline std::size_t plain_output_run(const unsigned char* s, std::size_t n) noexc return i + static_cast(count_trailing_zeros(mask)); } } + // NOLINTEND(portability-simd-intrinsics) #endif constexpr std::uint64_t ones = 0x0101010101010101ull; constexpr std::uint64_t high = 0x8080808080808080ull; @@ -372,11 +374,11 @@ template<> struct depth_counter { void enter() noexcept {} - std::size_t leave() noexcept + std::size_t leave() noexcept // NOLINT(readability-convert-member-functions-to-static): same interface as depth_counter { return 0; } - std::size_t get() const noexcept + std::size_t get() const noexcept // NOLINT(readability-convert-member-functions-to-static): same interface as depth_counter { return 0; } @@ -536,7 +538,8 @@ class view_serializer // room for k bytes const auto room = [&](std::size_t k) { - if (NLOHMANN_VIEW_UNLIKELY(static_cast(lim - w) < k)) + const auto left = static_cast(lim - w); + if (NLOHMANN_VIEW_UNLIKELY(left < k)) { m_out.set_cursor(w); m_out.reserve(k); diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index f3f3ad8ef..2ccc17cd6 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -6208,6 +6208,7 @@ inline std::size_t plain_output_run(const unsigned char* s, std::size_t n) noexc #if NLOHMANN_VIEW_SSE2 // (16 bytes per step; with NEON, the mask of a step costs more than it saves on the // short runs that are typical here) + // NOLINTBEGIN(portability-simd-intrinsics) for (; i + 16 <= n; i += 16) { const __m128i in = _mm_loadu_si128(static_cast(static_cast(s + i))); // NOLINT(bugprone-casting-through-void) @@ -6220,6 +6221,7 @@ inline std::size_t plain_output_run(const unsigned char* s, std::size_t n) noexc return i + static_cast(count_trailing_zeros(mask)); } } + // NOLINTEND(portability-simd-intrinsics) #endif constexpr std::uint64_t ones = 0x0101010101010101ull; constexpr std::uint64_t high = 0x8080808080808080ull; @@ -6439,11 +6441,11 @@ template<> struct depth_counter { void enter() noexcept {} - std::size_t leave() noexcept + std::size_t leave() noexcept // NOLINT(readability-convert-member-functions-to-static): same interface as depth_counter { return 0; } - std::size_t get() const noexcept + std::size_t get() const noexcept // NOLINT(readability-convert-member-functions-to-static): same interface as depth_counter { return 0; } @@ -6603,7 +6605,8 @@ class view_serializer // room for k bytes const auto room = [&](std::size_t k) { - if (NLOHMANN_VIEW_UNLIKELY(static_cast(lim - w) < k)) + const auto left = static_cast(lim - w); + if (NLOHMANN_VIEW_UNLIKELY(left < k)) { m_out.set_cursor(w); m_out.reserve(k); diff --git a/tests/src/unit-json_view_dump.cpp b/tests/src/unit-json_view_dump.cpp index 664e4608c..850dba3c7 100644 --- a/tests/src/unit-json_view_dump.cpp +++ b/tests/src/unit-json_view_dump.cpp @@ -200,11 +200,13 @@ TEST_CASE("json_view dump") { std::string content(length, 'a'); content.insert(position, special); - const std::string texts[] = + const std::array texts = { - "\"" + content + "\"", - "[\"" + content + "\"]", - "{\"" + content + "\":1}", + { + "\"" + content + "\"", + "[\"" + content + "\"]", + "{\"" + content + "\":1}", + } }; for (const std::string& text : texts) { @@ -291,7 +293,7 @@ TEST_CASE("json_view dump") std::string text; for (int i = 0; i < depth; ++i) { - text += i % 2 == 0 ? "[1," : "{\"k\":2,\"\\u00e9\":"; + text += i % 2 == 0 ? "[1," : R"({"k":2,"\u00e9":)"; } text += "[]"; for (int i = depth - 1; i >= 0; --i)