From 525787a2f7266e3c19adecc49b19383e7a4e1565 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 10 Oct 2026 01:46:20 +0200 Subject: [PATCH] Avoid GCC useless-cast and strict-overflow warnings in unit-json_view.cpp Signed-off-by: Niels Lohmann --- tests/src/unit-json_view.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/src/unit-json_view.cpp b/tests/src/unit-json_view.cpp index 3dec8d676..e95158346 100644 --- a/tests/src/unit-json_view.cpp +++ b/tests/src/unit-json_view.cpp @@ -861,6 +861,8 @@ TEST_CASE("json_view element access and iteration") const json_document d = json_document::parse("[10,20,30]"); const json_view v = d.root(); const json j = v.materialize(); + // (a variable: a cast of a constant to int32_t is a useless cast to GCC) + const std::int32_t int32_index = 1; // (compile-time: no overload is ambiguous) CHECK(v[0].materialize() == 10); CHECK(v[1].materialize() == 20); @@ -876,7 +878,7 @@ TEST_CASE("json_view element access and iteration") CHECK(v[static_cast(2)].materialize() == 30); CHECK(v[std::int8_t(1)].materialize() == 20); CHECK(v[std::int16_t(2)].materialize() == 30); - CHECK(v[std::int32_t(1)].materialize() == 20); + CHECK(v[int32_index].materialize() == 20); CHECK(v[std::int64_t(2)].materialize() == 30); CHECK(v[std::uint32_t(0)].materialize() == 10); CHECK(v[std::uint64_t(1)].materialize() == 20); @@ -891,7 +893,7 @@ TEST_CASE("json_view element access and iteration") CHECK(v.at(2ULL).materialize() == 30); CHECK(v.at(static_cast(1)).materialize() == 20); CHECK(v.at(static_cast(2)).materialize() == 30); - CHECK(v.at(std::int32_t(0)).materialize() == 10); + CHECK(v.at(int32_index - 1).materialize() == 10); CHECK(v.at(std::uint32_t(0)).materialize() == 10); CHECK(v.at(std::int64_t(0)).materialize() == 10); CHECK(v.at(std::uint64_t(1)).materialize() == 20); @@ -1819,7 +1821,7 @@ TEST_CASE("json_view large objects") for (int object = 0; object < objects; ++object) { text += object != 0 ? ",{" : "{"; - for (int i = 0; i < members + object; ++i) + for (int i = 0, n = members + object; i < n; ++i) { text += (i != 0 ? ",\"" : "\"") + std::to_string(i) + "\":" + std::to_string(i); } @@ -1846,7 +1848,7 @@ TEST_CASE("json_view large objects") for (int object = 0; object < 5; ++object) { const json_view v = d.root()[static_cast(object)]; - for (int i = 0; i < 150 + object; ++i) + for (int i = 0, n = 150 + object; i < n; ++i) { CHECK(v[std::to_string(i)].get() == i); }