From 282079ba8206dc17c1fa2e3e4da4ce4cab7401f9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 28 Oct 2026 19:44:53 +0100 Subject: [PATCH] Fix clang-tidy findings in unit-json_view_from_json.cpp (#5954) * Fix clang-tidy findings in unit-json_view_from_json.cpp Silence misc-use-internal-linkage on the macro-generated ADL functions, reconcile -Weffc++ with readability-redundant-member-init, use auto for get() initializers, and use value() on the optional access. Signed-off-by: Niels Lohmann * Satisfy bugprone-unchecked-optional-access in unit-json_view_from_json.cpp clang-tidy 22 also flags value(); check has_value() in the same expression. Signed-off-by: Niels Lohmann --------- Signed-off-by: Niels Lohmann --- tests/src/unit-json_view_from_json.cpp | 31 +++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/src/unit-json_view_from_json.cpp b/tests/src/unit-json_view_from_json.cpp index 4a776dd52..c1c8925d8 100644 --- a/tests/src/unit-json_view_from_json.cpp +++ b/tests/src/unit-json_view_from_json.cpp @@ -69,9 +69,12 @@ struct point struct shape { // initialized for GCC -Weffc++ + // NOLINTNEXTLINE(readability-redundant-member-init) std::string name{}; + // NOLINTNEXTLINE(readability-redundant-member-init) std::vector points{}; point center{}; + // NOLINTNEXTLINE(readability-redundant-member-init) std::map named{}; NLOHMANN_DEFINE_TYPE_INTRUSIVE(shape, name, points, center, named) }; @@ -82,13 +85,17 @@ struct with_defaults std::string b = "default"; std::vector c = {1, 2}; }; +// NOLINTNEXTLINE(misc-use-internal-linkage): found by ADL, like the from_json() functions of users NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(with_defaults, a, b, c) struct with_names { int value = 0; - std::string text{}; // initialized for GCC -Weffc++ + // initialized for GCC -Weffc++ + // NOLINTNEXTLINE(readability-redundant-member-init) + std::string text{}; }; +// NOLINTNEXTLINE(misc-use-internal-linkage): found by ADL, like the from_json() functions of users NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_NAMES(with_names, "Value", value, "Text", text) struct empty @@ -102,6 +109,7 @@ enum class color green, blue }; +// NOLINTNEXTLINE(misc-use-internal-linkage): found by ADL, like the from_json() functions of users NLOHMANN_JSON_SERIALIZE_ENUM(color, { {color::red, "red"}, @@ -114,6 +122,7 @@ enum class strict_color red, green }; +// NOLINTNEXTLINE(misc-use-internal-linkage): found by ADL, like the from_json() functions of users NLOHMANN_JSON_SERIALIZE_ENUM_STRICT(strict_color, { {strict_color::red, "red"}, @@ -154,6 +163,7 @@ struct derived_from_legacy : legacy_base { int extra = 0; }; +// NOLINTNEXTLINE(misc-use-internal-linkage): found by ADL, like the from_json() functions of users NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(derived_from_legacy, legacy_base, extra) // from_json() for json only: converted through materialize() @@ -301,18 +311,19 @@ TEST_CASE("json_view from_json") CHECK(same(v, j, "text")); CHECK(same(v, j, "nothing")); CHECK(same(v, j, "object")); - const std::valarray va = v["ints"].get>(); + const auto va = v["ints"].get>(); CHECK(va.size() == 4); CHECK(va[0] == 3); int c_array[2][2] = {{0, 0}, {0, 0}}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays): the conversion to C arrays v["nested"][0].get_to(c_array[0]); CHECK(c_array[0][1] == 2); #ifdef JSON_HAS_CPP_17 - CHECK(v["points"][0].get>()->y == 2); + const auto first = v["points"][0].get>(); + CHECK((first.has_value() && first->y == 2)); CHECK(!v["nothing"].get>().has_value()); #endif // containers of views keep views of the elements - const std::vector views = v["points"].get>(); + const auto views = v["points"].get>(); CHECK(views.size() == 2); CHECK(views[1]["x"].get() == 3); CHECK(v["object"].get>().at("a").get() == 2); @@ -352,7 +363,7 @@ TEST_CASE("json_view from_json") { const json_document d = json_document::parse(R"({"name": "s", "points": [{"x": 1, "y": 2}, {"x": 3, "y": 4}], "center": {"x": 5, "y": 6}, "named": {"a": {"x": 7, "y": 8}}})"); - const shape s = d.root().get(); + const auto s = d.root().get(); CHECK(s.name == "s"); CHECK(s.points.size() == 2); CHECK(s.points[1].y == 4); @@ -370,13 +381,13 @@ TEST_CASE("json_view from_json") SECTION("defaults and names") { const json_document d = json_document::parse(R"({"a": 5, "Value": 3, "Text": "t"})"); - const with_defaults w = d.root().get(); + const auto w = d.root().get(); CHECK(w.a == 5); CHECK(w.b == "default"); CHECK(w.c == std::vector {1, 2}); const json_document null_document = json_document::parse("null"); CHECK(null_document.root().get().a == 1); - const with_names n = d.root().get(); + const auto n = d.root().get(); CHECK(n.value == 3); CHECK(n.text == "t"); } @@ -401,13 +412,13 @@ TEST_CASE("json_view from_json") SECTION("derived types") { const json_document d = json_document::parse(R"({"id": 7, "c": "green", "extra": 3})"); - const derived x = d.root().get(); + const auto x = d.root().get(); CHECK(x.id == 7); CHECK(x.c == color::green); - const derived_with_defaults y = d.root().get(); + const auto y = d.root().get(); CHECK(y.id == 7); CHECK(y.label == "none"); - const derived_from_legacy z = d.root().get(); + const auto z = d.root().get(); CHECK(z.id == 7); CHECK(z.extra == 3); }