diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 634615d35..1fb1c052b 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -330,7 +330,7 @@ class binary_reader { std::array cr{{}}; static_cast((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::string cr_str{cr.data()}; + const std::string cr_str{cr.data()}; return sax->parse_error(element_type_parse_position, cr_str, parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr)); } @@ -2265,7 +2265,7 @@ class binary_reader exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr)); } - bool is_error = get_ubjson_size_value(result.first, is_ndarray); + const bool is_error = get_ubjson_size_value(result.first, is_ndarray); if (input_format == input_format_t::bjdata && is_ndarray) { if (inside_ndarray) @@ -2280,7 +2280,7 @@ class binary_reader if (current == '#') { - bool is_error = get_ubjson_size_value(result.first, is_ndarray); + const bool is_error = get_ubjson_size_value(result.first, is_ndarray); if (input_format == input_format_t::bjdata && is_ndarray) { return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read, diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7a2f9dabb..40e84d43f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -9408,7 +9408,7 @@ class binary_reader { std::array cr{{}}; static_cast((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::string cr_str{cr.data()}; + const std::string cr_str{cr.data()}; return sax->parse_error(element_type_parse_position, cr_str, parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr)); } @@ -11343,7 +11343,7 @@ class binary_reader exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr)); } - bool is_error = get_ubjson_size_value(result.first, is_ndarray); + const bool is_error = get_ubjson_size_value(result.first, is_ndarray); if (input_format == input_format_t::bjdata && is_ndarray) { if (inside_ndarray) @@ -11358,7 +11358,7 @@ class binary_reader if (current == '#') { - bool is_error = get_ubjson_size_value(result.first, is_ndarray); + const bool is_error = get_ubjson_size_value(result.first, is_ndarray); if (input_format == input_format_t::bjdata && is_ndarray) { return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read, diff --git a/tests/abi/config/custom.cpp b/tests/abi/config/custom.cpp index a004fcca2..87d3aa6ea 100644 --- a/tests/abi/config/custom.cpp +++ b/tests/abi/config/custom.cpp @@ -25,7 +25,7 @@ TEST_CASE("custom namespace") std::string expected = "nlohmann::basic_json"; // fallback for Clang - std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; + const std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; CHECK(namespace_name(ns) == expected); } diff --git a/tests/abi/config/default.cpp b/tests/abi/config/default.cpp index 908314acf..51bf0c3bb 100644 --- a/tests/abi/config/default.cpp +++ b/tests/abi/config/default.cpp @@ -33,7 +33,7 @@ TEST_CASE("default namespace") expected += "_" STRINGIZE(NLOHMANN_JSON_VERSION_PATCH) "::basic_json"; // fallback for Clang - std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; + const std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; CHECK(namespace_name(ns) == expected); } diff --git a/tests/abi/config/noversion.cpp b/tests/abi/config/noversion.cpp index d4fcb3f46..e6b4f7721 100644 --- a/tests/abi/config/noversion.cpp +++ b/tests/abi/config/noversion.cpp @@ -32,7 +32,7 @@ TEST_CASE("default namespace without version component") expected += "::basic_json"; // fallback for Clang - std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; + const std::string ns{STRINGIZE(NLOHMANN_JSON_NAMESPACE) "::basic_json"}; CHECK(namespace_name(ns) == expected); } diff --git a/tests/src/unit-comparison.cpp b/tests/src/unit-comparison.cpp index b838c02f2..7f990d252 100644 --- a/tests/src/unit-comparison.cpp +++ b/tests/src/unit-comparison.cpp @@ -391,7 +391,7 @@ TEST_CASE("lexicographical comparison operators") } // compare with null pointer - json j_null; + const json j_null; CHECK((j_null != nullptr) == false); CHECK((nullptr != j_null) == false); CHECK((j_null != nullptr) == !(j_null == nullptr)); diff --git a/tests/src/unit-concepts.cpp b/tests/src/unit-concepts.cpp index 3a0c81b05..e96caba7e 100644 --- a/tests/src/unit-concepts.cpp +++ b/tests/src/unit-concepts.cpp @@ -57,7 +57,7 @@ TEST_CASE("concepts") // the expression "X u" has the post-condition "u.empty()" { - json u; + const json u; CHECK(u.empty()); } diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 78ed3bb3e..24d89e11a 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -36,7 +36,7 @@ TEST_CASE("value conversion") { SECTION("get an object (explicit)") { - json::object_t o_reference = {{"object", json::object()}, + const json::object_t o_reference = {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, @@ -53,28 +53,28 @@ TEST_CASE("value conversion") SECTION("std::map") { - std::map o = + const std::map o = j.get>(); CHECK(json(o) == j); } SECTION("std::multimap") { - std::multimap o = + const std::multimap o = j.get>(); CHECK(json(o) == j); } SECTION("std::unordered_map") { - std::unordered_map o = + const std::unordered_map o = j.get>(); CHECK(json(o) == j); } SECTION("std::unordered_multimap") { - std::unordered_multimap o = + const std::unordered_multimap o = j.get>(); CHECK(json(o) == j); } @@ -107,7 +107,7 @@ TEST_CASE("value conversion") SECTION("get an object (explicit, get_to)") { - json::object_t o_reference = {{"object", json::object()}, + const json::object_t o_reference = {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, @@ -155,7 +155,7 @@ TEST_CASE("value conversion") #if JSON_USE_IMPLICIT_CONVERSIONS SECTION("get an object (implicit)") { - json::object_t o_reference = {{"object", json::object()}, + const json::object_t o_reference = {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, @@ -166,31 +166,31 @@ TEST_CASE("value conversion") SECTION("json::object_t") { - json::object_t o = j; + const json::object_t o = j; CHECK(json(o) == j); } SECTION("std::map") { - std::map o = j; + const std::map o = j; CHECK(json(o) == j); } SECTION("std::multimap") { - std::multimap o = j; + const std::multimap o = j; CHECK(json(o) == j); } SECTION("std::unordered_map") { - std::unordered_map o = j; + const std::unordered_map o = j; CHECK(json(o) == j); } SECTION("std::unordered_multimap") { - std::unordered_multimap o = j; + const std::unordered_multimap o = j; CHECK(json(o) == j); } } @@ -198,25 +198,25 @@ TEST_CASE("value conversion") SECTION("get an array (explicit)") { - json::array_t a_reference{json(1), json(1u), json(2.2), - json(false), json("string"), json()}; + const json::array_t a_reference{json(1), json(1u), json(2.2), + json(false), json("string"), json()}; json j(a_reference); SECTION("json::array_t") { - json::array_t a = j.get(); + const json::array_t a = j.get(); CHECK(json(a) == j); } SECTION("std::list") { - std::list a = j.get>(); + const std::list a = j.get>(); CHECK(json(a) == j); } SECTION("std::forward_list") { - std::forward_list a = j.get>(); + const std::forward_list a = j.get>(); CHECK(json(a) == j); CHECK_THROWS_WITH_AS( @@ -226,7 +226,7 @@ TEST_CASE("value conversion") SECTION("std::vector") { - std::vector a = j.get>(); + const std::vector a = j.get>(); CHECK(json(a) == j); CHECK_THROWS_WITH_AS( @@ -237,7 +237,7 @@ TEST_CASE("value conversion") SECTION("reserve is called on containers that supports it") { // make sure all values are properly copied - json j2({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); + const json j2({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); auto v2 = j2.get>(); CHECK(v2.size() == 10); } @@ -249,8 +249,8 @@ TEST_CASE("value conversion") const char str[] = "a string"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) const int nbs[] = {0, 1, 2}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - json j2 = nbs; - json j3 = str; + const json j2 = nbs; + const json j3 = str; auto v = j2.get>(); auto s = j3.get(); @@ -260,7 +260,7 @@ TEST_CASE("value conversion") SECTION("std::deque") { - std::deque a = j.get>(); + const std::deque a = j.get>(); CHECK(json(a) == j); } @@ -295,8 +295,8 @@ TEST_CASE("value conversion") SECTION("get an array (explicit, get_to)") { - json::array_t a_reference{json(1), json(1u), json(2.2), - json(false), json("string"), json()}; + const json::array_t a_reference{json(1), json(1u), json(2.2), + json(false), json("string"), json()}; json j(a_reference); SECTION("json::array_t") @@ -339,7 +339,7 @@ TEST_CASE("value conversion") const int nbs[] = {0, 1, 2}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) int nbs2[] = {0, 0, 0}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - json j2 = nbs; + const json j2 = nbs; j2.get_to(nbs2); CHECK(std::equal(std::begin(nbs), std::end(nbs), std::begin(nbs2))); } @@ -355,37 +355,37 @@ TEST_CASE("value conversion") #if JSON_USE_IMPLICIT_CONVERSIONS SECTION("get an array (implicit)") { - json::array_t a_reference{json(1), json(1u), json(2.2), - json(false), json("string"), json()}; + const json::array_t a_reference{json(1), json(1u), json(2.2), + json(false), json("string"), json()}; json j(a_reference); SECTION("json::array_t") { - json::array_t a = j; + const json::array_t a = j; CHECK(json(a) == j); } SECTION("std::list") { - std::list a = j; + const std::list a = j; CHECK(json(a) == j); } SECTION("std::forward_list") { - std::forward_list a = j; + const std::forward_list a = j; CHECK(json(a) == j); } SECTION("std::vector") { - std::vector a = j; + const std::vector a = j; CHECK(json(a) == j); } SECTION("std::deque") { - std::deque a = j; + const std::deque a = j; CHECK(json(a) == j); } } @@ -393,18 +393,18 @@ TEST_CASE("value conversion") SECTION("get a string (explicit)") { - json::string_t s_reference{"Hello world"}; + const json::string_t s_reference{"Hello world"}; json j(s_reference); SECTION("string_t") { - json::string_t s = j.get(); + const json::string_t s = j.get(); CHECK(json(s) == j); } SECTION("std::string") { - std::string s = j.get(); + const std::string s = j.get(); CHECK(json(s) == j); } #if defined(JSON_HAS_CPP_17) @@ -463,7 +463,7 @@ TEST_CASE("value conversion") SECTION("get a string (explicit, get_to)") { - json::string_t s_reference{"Hello world"}; + const json::string_t s_reference{"Hello world"}; json j(s_reference); SECTION("string_t") @@ -493,7 +493,7 @@ TEST_CASE("value conversion") SECTION("get null (explicit)") { std::nullptr_t n = nullptr; - json j(n); + const json j(n); auto n2 = j.get(); CHECK(n2 == n); @@ -517,12 +517,12 @@ TEST_CASE("value conversion") #if JSON_USE_IMPLICIT_CONVERSIONS SECTION("get a string (implicit)") { - json::string_t s_reference{"Hello world"}; + const json::string_t s_reference{"Hello world"}; json j(s_reference); SECTION("string_t") { - json::string_t s = j; + const json::string_t s = j; CHECK(json(s) == j); } @@ -536,7 +536,7 @@ TEST_CASE("value conversion") SECTION("std::string") { - std::string s = j; + const std::string s = j; CHECK(json(s) == j); } } @@ -544,7 +544,7 @@ TEST_CASE("value conversion") SECTION("get a boolean (explicit)") { - json::boolean_t b_reference{true}; + const json::boolean_t b_reference{true}; json j(b_reference); SECTION("boolean_t") @@ -561,7 +561,7 @@ TEST_CASE("value conversion") SECTION("bool") { - bool b = j.get(); + const bool b = j.get(); CHECK(json(b) == j); } @@ -600,18 +600,18 @@ TEST_CASE("value conversion") #if JSON_USE_IMPLICIT_CONVERSIONS SECTION("get a boolean (implicit)") { - json::boolean_t b_reference{true}; + const json::boolean_t b_reference{true}; json j(b_reference); SECTION("boolean_t") { - json::boolean_t b = j; + const json::boolean_t b = j; CHECK(json(b) == j); } SECTION("bool") { - bool b = j; + const bool b = j; CHECK(json(b) == j); } } @@ -619,9 +619,9 @@ TEST_CASE("value conversion") SECTION("get an integer number (explicit)") { - json::number_integer_t n_reference{42}; + const json::number_integer_t n_reference{42}; json j(n_reference); - json::number_unsigned_t n_unsigned_reference{42u}; + const json::number_unsigned_t n_unsigned_reference{42u}; json j_unsigned(n_unsigned_reference); SECTION("number_integer_t") @@ -650,7 +650,7 @@ TEST_CASE("value conversion") SECTION("int") { - int n = j.get(); + const int n = j.get(); CHECK(json(n) == j); } @@ -662,7 +662,7 @@ TEST_CASE("value conversion") SECTION("long") { - long n = j.get(); + const long n = j.get(); CHECK(json(n) == j); } diff --git a/tests/src/unit-reference_access.cpp b/tests/src/unit-reference_access.cpp index 5f450ec2c..32a4097dd 100644 --- a/tests/src/unit-reference_access.cpp +++ b/tests/src/unit-reference_access.cpp @@ -14,7 +14,7 @@ using nlohmann::json; TEST_CASE("reference access") { // create a JSON value with different types - json json_types = + const json json_types = { {"boolean", true}, { diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index 503d2a4f8..308ceeef9 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -348,7 +348,7 @@ TEST_CASE("regression tests 2") ] })"; - json::parser_callback_t cb = [&](int /*level*/, json::parse_event_t event, json & parsed) noexcept + const json::parser_callback_t cb = [&](int /*level*/, json::parse_event_t event, json & parsed) noexcept { // skip uninteresting events if (event == json::parse_event_t::value && !parsed.is_primitive()) @@ -440,7 +440,7 @@ TEST_CASE("regression tests 2") SECTION("issue #1299 - compile error in from_json converting to container " "with std::pair") { - json j = + const json j = { {"1", {{"a", "testa_1"}, {"b", "testb_1"}}}, {"2", {{"a", "testa_2"}, {"b", "testb_2"}}}, @@ -501,7 +501,7 @@ TEST_CASE("regression tests 2") {109, 108, 103, 125, -122, -53, 115, 18, 3, 0, 102, 19, 1, 15, -110, 13, -3, -1, -81, 32, 2, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -80, 2, 0, 0, 96, -118, 46, -116, 46, 109, -84, -87, 108, 14, 109, -24, -83, 13, -18, -51, -83, -52, -115, 14, 6, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 0, 0, 0, 35, -74, -73, 55, 57, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 0, 0, 0, -96, -54, -28, -26} }; std::string s; - for (int i : data) + for (const int i : data) { s += static_cast(i); } @@ -512,20 +512,20 @@ TEST_CASE("regression tests 2") SECTION("issue #1447 - Integer Overflow (OSS-Fuzz 12506)") { - json j = json::parse("[-9223372036854775808]"); + const json j = json::parse("[-9223372036854775808]"); CHECK(j.dump() == "[-9223372036854775808]"); } SECTION("issue #1708 - minimum value of int64_t can be outputted") { constexpr auto smallest = (std::numeric_limits::min)(); - json j = smallest; + const json j = smallest; CHECK(j.dump() == std::to_string(smallest)); } SECTION("issue #1727 - Contains with non-const lvalue json_pointer picks the wrong overload") { - json j = {{"root", {{"settings", {{"logging", true}}}}}}; + const json j = {{"root", {{"settings", {{"logging", true}}}}}}; auto jptr1 = "/root/settings/logging"_json_pointer; auto jptr2 = json::json_pointer{"/root/settings/logging"}; @@ -539,7 +539,7 @@ TEST_CASE("regression tests 2") // does not compile on ICPC when targeting C++20 #if !(defined(__INTEL_COMPILER) && __cplusplus >= 202000) { - json j; + const json j; NonDefaultFromJsonStruct x(j); NonDefaultFromJsonStruct y; CHECK(x == y); @@ -548,27 +548,27 @@ TEST_CASE("regression tests 2") auto val = nlohmann::json("one").get(); CHECK(val == for_1647::one); - json j = val; + const json j = val; } SECTION("issue #1715 - json::from_cbor does not respect allow_exceptions = false when input is string literal") { SECTION("string literal") { - json cbor = json::from_cbor("B", true, false); + const json cbor = json::from_cbor("B", true, false); CHECK(cbor.is_discarded()); } SECTION("string array") { const std::array input = {{'B', 0x00}}; - json cbor = json::from_cbor(input, true, false); + const json cbor = json::from_cbor(input, true, false); CHECK(cbor.is_discarded()); } SECTION("std::string") { - json cbor = json::from_cbor(std::string("B"), true, false); + const json cbor = json::from_cbor(std::string("B"), true, false); CHECK(cbor.is_discarded()); } } @@ -597,7 +597,7 @@ TEST_CASE("regression tests 2") SECTION("issue #2067 - cannot serialize binary data to text JSON") { const std::array data = {{0x81, 0xA4, 0x64, 0x61, 0x74, 0x61, 0xC4, 0x0F, 0x33, 0x30, 0x30, 0x32, 0x33, 0x34, 0x30, 0x31, 0x30, 0x37, 0x30, 0x35, 0x30, 0x31, 0x30}}; - json j = json::from_msgpack(data.data(), data.size()); + const json j = json::from_msgpack(data.data(), data.size()); CHECK_NOTHROW( j.dump(4, // Indent ' ', // Indent char @@ -609,15 +609,15 @@ TEST_CASE("regression tests 2") SECTION("PR #2181 - regression bug with lvalue") { // see https://github.com/nlohmann/json/pull/2181#issuecomment-653326060 - json j{{"x", "test"}}; - std::string defval = "default value"; + const json j{{"x", "test"}}; + const std::string defval = "default value"; auto val = j.value("x", defval); auto val2 = j.value("y", defval); } SECTION("issue #2293 - eof doesn't cause parsing to stop") { - std::vector data = + const std::vector data = { 0x7B, 0x6F, @@ -630,23 +630,23 @@ TEST_CASE("regression tests 2") 0x4F, 0x42 }; - json result = json::from_cbor(data, true, false); + const json result = json::from_cbor(data, true, false); CHECK(result.is_discarded()); } SECTION("issue #2315 - json.update and vectordoes not work with ordered_json") { nlohmann::ordered_json jsonAnimals = {{"animal", "dog"}}; - nlohmann::ordered_json jsonCat = {{"animal", "cat"}}; + const nlohmann::ordered_json jsonCat = {{"animal", "cat"}}; jsonAnimals.update(jsonCat); CHECK(jsonAnimals["animal"] == "cat"); auto jsonAnimals_parsed = nlohmann::ordered_json::parse(jsonAnimals.dump()); CHECK(jsonAnimals == jsonAnimals_parsed); - std::vector> intData = {std::make_pair("aaaa", 11), - std::make_pair("bbb", 222) - }; + const std::vector> intData = {std::make_pair("aaaa", 11), + std::make_pair("bbb", 222) + }; nlohmann::ordered_json jsonObj; for (const auto& data : intData) { @@ -658,8 +658,8 @@ TEST_CASE("regression tests 2") SECTION("issue #2330 - ignore_comment=true fails on multiple consecutive lines starting with comments") { - std::string ss = "//\n//\n{\n}\n"; - json j = json::parse(ss, nullptr, true, true); + const std::string ss = "//\n//\n{\n}\n"; + const json j = json::parse(ss, nullptr, true, true); CHECK(j.dump() == "{}"); } @@ -668,7 +668,7 @@ TEST_CASE("regression tests 2") { const char DATA[] = R"("Hello, world!")"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) const auto s = std::as_bytes(std::span(DATA)); - json j = json::parse(s); + const json j = json::parse(s); CHECK(j.dump() == "\"Hello, world!\""); } #endif @@ -678,14 +678,14 @@ TEST_CASE("regression tests 2") SECTION("std::array") { { - json j = {7, 4}; + const json j = {7, 4}; auto arr = j.get>(); CHECK(arr[0].x == 7); CHECK(arr[1].x == 4); } { - json j = 7; + const json j = 7; CHECK_THROWS_AS((j.get>()), json::type_error); } } @@ -693,28 +693,28 @@ TEST_CASE("regression tests 2") SECTION("std::pair") { { - json j = {3, 8}; + const json j = {3, 8}; auto p = j.get>(); CHECK(p.first.x == 3); CHECK(p.second.x == 8); } { - json j = {4, 1}; + const json j = {4, 1}; auto p = j.get>(); CHECK(p.first == 4); CHECK(p.second.x == 1); } { - json j = {6, 7}; + const json j = {6, 7}; auto p = j.get>(); CHECK(p.first.x == 6); CHECK(p.second == 7); } { - json j = 7; + const json j = 7; CHECK_THROWS_AS((j.get>()), json::type_error); } } @@ -722,13 +722,13 @@ TEST_CASE("regression tests 2") SECTION("std::tuple") { { - json j = {9}; + const json j = {9}; auto t = j.get>(); CHECK(std::get<0>(t).x == 9); } { - json j = {9, 8, 7}; + const json j = {9, 8, 7}; auto t = j.get>(); CHECK(std::get<0>(t).x == 9); CHECK(std::get<1>(t) == 8); @@ -736,7 +736,7 @@ TEST_CASE("regression tests 2") } { - json j = 7; + const json j = 7; CHECK_THROWS_AS((j.get>()), json::type_error); } } @@ -747,7 +747,7 @@ TEST_CASE("regression tests 2") // the code below is expected to not leak memory { nlohmann::json o; - std::string s = "bar"; + const std::string s = "bar"; nlohmann::to_json(o["foo"], s); @@ -759,7 +759,7 @@ TEST_CASE("regression tests 2") { nlohmann::json o; - std::string s = "bar"; + const std::string s = "bar"; nlohmann::to_json(o["foo"], s); @@ -785,7 +785,7 @@ TEST_CASE("regression tests 2") SECTION("issue #2958 - Inserting in unordered json using a pointer retains the leading slash") { - std::string p = "/root"; + const std::string p = "/root"; json test1; test1[json::json_pointer(p)] = json::object(); @@ -816,7 +816,7 @@ TEST_CASE("regression tests 2") SECTION("issue #3070 - Version 3.10.3 breaks backward-compatibility with 3.10.2 ") { nlohmann::detail::std_fs::path text_path("/tmp/text.txt"); - json j(text_path); + const json j(text_path); const auto j_path = j.get(); CHECK(j_path == text_path); @@ -867,7 +867,7 @@ TEST_CASE("regression tests 2") SECTION("issue #3171 - if class is_constructible from std::string wrong from_json overload is being selected, compilation failed") { - json j{{ "str", "value"}}; + const json j{{ "str", "value"}}; // failed with: error: no match for ‘operator=’ (operand types are ‘for_3171_derived’ and ‘const nlohmann::basic_json<>::string_t’ // {aka ‘const std::__cxx11::basic_string’}) @@ -881,7 +881,7 @@ TEST_CASE("regression tests 2") SECTION("issue #3312 - Parse to custom class from unordered_json breaks on G++11.2.0 with C++20") { // see test for #3171 - ordered_json j = {{"name", "class"}}; + const ordered_json j = {{"name", "class"}}; for_3312 obj{}; j.get_to(obj); @@ -893,8 +893,8 @@ TEST_CASE("regression tests 2") #if defined(JSON_HAS_CPP_17) && JSON_USE_IMPLICIT_CONVERSIONS SECTION("issue #3428 - Error occurred when converting nlohmann::json to std::any") { - json j; - std::any a1 = j; + const json j; + const std::any a1 = j; std::any&& a2 = j; CHECK(a1.type() == typeid(j)); diff --git a/tests/src/unit-serialization.cpp b/tests/src/unit-serialization.cpp index a023d23d9..01e5bdfea 100644 --- a/tests/src/unit-serialization.cpp +++ b/tests/src/unit-serialization.cpp @@ -21,7 +21,7 @@ TEST_CASE("serialization") SECTION("no given width") { std::stringstream ss; - json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; + const json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; ss << j; CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]"); } @@ -29,7 +29,7 @@ TEST_CASE("serialization") SECTION("given width") { std::stringstream ss; - json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; + const json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; ss << std::setw(4) << j; CHECK(ss.str() == "[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]"); @@ -38,7 +38,7 @@ TEST_CASE("serialization") SECTION("given fill") { std::stringstream ss; - json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; + const json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; ss << std::setw(1) << std::setfill('\t') << j; CHECK(ss.str() == "[\n\t\"foo\",\n\t1,\n\t2,\n\t3,\n\tfalse,\n\t{\n\t\t\"one\": 1\n\t}\n]"); @@ -50,7 +50,7 @@ TEST_CASE("serialization") SECTION("no given width") { std::stringstream ss; - json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; + const json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; j >> ss; CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]"); } @@ -58,7 +58,7 @@ TEST_CASE("serialization") SECTION("given width") { std::stringstream ss; - json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; + const json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; ss.width(4); j >> ss; CHECK(ss.str() == @@ -68,7 +68,7 @@ TEST_CASE("serialization") SECTION("given fill") { std::stringstream ss; - json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; + const json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; ss.width(1); ss.fill('\t'); j >> ss; @@ -81,7 +81,7 @@ TEST_CASE("serialization") { SECTION("invalid character") { - json j = "ä\xA9ü"; + const json j = "ä\xA9ü"; CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9", json::type_error&); CHECK_THROWS_WITH_AS(j.dump(1, ' ', false, json::error_handler_t::strict), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9", json::type_error&); @@ -92,7 +92,7 @@ TEST_CASE("serialization") SECTION("ending with incomplete character") { - json j = "123\xC2"; + const json j = "123\xC2"; CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2", json::type_error&); CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&); @@ -103,7 +103,7 @@ TEST_CASE("serialization") SECTION("unexpected character") { - json j = "123\xF1\xB0\x34\x35\x36"; + const json j = "123\xF1\xB0\x34\x35\x36"; CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 5: 0x34", json::type_error&); CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&); @@ -120,7 +120,7 @@ TEST_CASE("serialization") auto test = [&](std::string const & input, std::string const & expected) { - json j = input; + const json j = input; CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"" + expected + "\""); }; @@ -154,7 +154,7 @@ TEST_CASE("serialization") auto test = [&](std::string const & input, std::string const & expected) { using std::to_string; - json j = input; + const json j = input; CHECK(to_string(j) == "\"" + expected + "\""); }; @@ -170,14 +170,14 @@ TEST_CASE_TEMPLATE("serialization for extreme integer values", T, int32_t, uint3 SECTION("minimum") { constexpr auto minimum = (std::numeric_limits::min)(); - json j = minimum; + const json j = minimum; CHECK(j.dump() == std::to_string(minimum)); } SECTION("maximum") { constexpr auto maximum = (std::numeric_limits::max)(); - json j = maximum; + const json j = maximum; CHECK(j.dump() == std::to_string(maximum)); } } @@ -189,15 +189,15 @@ TEST_CASE("dump with binary values") auto binary_with_subtype = json::binary({1, 2, 3, 4}, 128); auto binary_empty_with_subtype = json::binary({}, 128); - json object = {{"key", binary}}; - json object_empty = {{"key", binary_empty}}; - json object_with_subtype = {{"key", binary_with_subtype}}; - json object_empty_with_subtype = {{"key", binary_empty_with_subtype}}; + const json object = {{"key", binary}}; + const json object_empty = {{"key", binary_empty}}; + const json object_with_subtype = {{"key", binary_with_subtype}}; + const json object_empty_with_subtype = {{"key", binary_empty_with_subtype}}; - json array = {"value", 1, binary}; - json array_empty = {"value", 1, binary_empty}; - json array_with_subtype = {"value", 1, binary_with_subtype}; - json array_empty_with_subtype = {"value", 1, binary_empty_with_subtype}; + const json array = {"value", 1, binary}; + const json array_empty = {"value", 1, binary_empty}; + const json array_with_subtype = {"value", 1, binary_with_subtype}; + const json array_empty_with_subtype = {"value", 1, binary_empty_with_subtype}; SECTION("normal") { diff --git a/tests/src/unit-testsuites.cpp b/tests/src/unit-testsuites.cpp index 48cb07255..e0cc127b9 100644 --- a/tests/src/unit-testsuites.cpp +++ b/tests/src/unit-testsuites.cpp @@ -302,7 +302,7 @@ TEST_CASE("compliance tests from nativejson-benchmark") (std::istreambuf_iterator()) ); CAPTURE(json_string) - json j = json::parse(json_string); + const json j = json::parse(json_string); CHECK(j.dump() == json_string); } } @@ -363,35 +363,35 @@ TEST_CASE("json.org examples") } SECTION("FILE 1.json") { - std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/1.json", "r"), &std::fclose); + const std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/1.json", "r"), &std::fclose); json _; CHECK_NOTHROW(_ = json::parse(f.get())); } SECTION("FILE 2.json") { - std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/2.json", "r"), &std::fclose); + const std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/2.json", "r"), &std::fclose); json _; CHECK_NOTHROW(_ = json::parse(f.get())); } SECTION("FILE 3.json") { - std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/3.json", "r"), &std::fclose); + const std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/3.json", "r"), &std::fclose); json _; CHECK_NOTHROW(_ = json::parse(f.get())); } SECTION("FILE 4.json") { - std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/4.json", "r"), &std::fclose); + const std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/4.json", "r"), &std::fclose); json _; CHECK_NOTHROW(_ = json::parse(f.get())); } SECTION("FILE 5.json") { - std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/5.json", "r"), &std::fclose); + const std::unique_ptr f(std::fopen(TEST_DATA_DIRECTORY "/json.org/5.json", "r"), &std::fclose); json _; CHECK_NOTHROW(_ = json::parse(f.get())); } @@ -1340,12 +1340,12 @@ std::string trim(const std::string& str); // from https://stackoverflow.com/a/25829178/266378 std::string trim(const std::string& str) { - size_t first = str.find_first_not_of(' '); + const size_t first = str.find_first_not_of(' '); if (std::string::npos == first) { return str; } - size_t last = str.find_last_not_of(' '); + const size_t last = str.find_last_not_of(' '); return str.substr(first, (last - first + 1)); } } // namespace @@ -1384,7 +1384,7 @@ TEST_CASE("Big List of Naughty Strings") // check roundtrip CAPTURE(line) - json j = json::parse(line); + const json j = json::parse(line); CHECK(j.dump() == line); } } diff --git a/tests/src/unit-to_chars.cpp b/tests/src/unit-to_chars.cpp index 0d5fcd422..d9f75ea2f 100644 --- a/tests/src/unit-to_chars.cpp +++ b/tests/src/unit-to_chars.cpp @@ -61,11 +61,11 @@ float make_float(uint64_t f, int e) e--; } - uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0) - ? 0 - : static_cast(e + kExponentBias); + const uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0) + ? 0 + : static_cast(e + kExponentBias); - uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize); + const uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize); return reinterpret_bits(static_cast(bits)); } @@ -113,11 +113,11 @@ double make_double(uint64_t f, int e) e--; } - uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0) - ? 0 - : static_cast(e + kExponentBias); + const uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0) + ? 0 + : static_cast(e + kExponentBias); - uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize); + const uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize); return reinterpret_bits(bits); } } // namespace @@ -458,7 +458,7 @@ TEST_CASE("formatting") { auto check_integer = [](std::int64_t number, const std::string & expected) { - nlohmann::json j = number; + const nlohmann::json j = number; CHECK(j.dump() == expected); }; diff --git a/tests/src/unit-unicode1.cpp b/tests/src/unit-unicode1.cpp index 0b87cfa9d..f7a6ad197 100644 --- a/tests/src/unit-unicode1.cpp +++ b/tests/src/unit-unicode1.cpp @@ -233,9 +233,9 @@ void roundtrip(bool success_expected, const std::string& s) json _; // create JSON string value - json j = s; + const json j = s; // create JSON text - std::string ps = std::string("\"") + s + "\""; + const std::string ps = std::string("\"") + s + "\""; if (success_expected) { @@ -253,7 +253,7 @@ void roundtrip(bool success_expected, const std::string& s) CHECK_NOTHROW(_ = json::parse(j.dump())); // after roundtrip, the same string is stored - json jr = json::parse(j.dump()); + const json jr = json::parse(j.dump()); CHECK(jr.get() == s); } else diff --git a/tests/src/unit-user_defined_input.cpp b/tests/src/unit-user_defined_input.cpp index febc14c7c..576c81ea9 100644 --- a/tests/src/unit-user_defined_input.cpp +++ b/tests/src/unit-user_defined_input.cpp @@ -45,7 +45,7 @@ const char* end(const MyContainer& c) TEST_CASE("Custom container non-member begin/end") { - MyContainer data{"[1,2,3,4]"}; + const MyContainer data{"[1,2,3,4]"}; json as_json = json::parse(data); CHECK(as_json.at(0) == 1); CHECK(as_json.at(1) == 2); @@ -71,7 +71,7 @@ TEST_CASE("Custom container member begin/end") } }; - MyContainer2 data{"[1,2,3,4]"}; + const MyContainer2 data{"[1,2,3,4]"}; json as_json = json::parse(data); CHECK(as_json.at(0) == 1); CHECK(as_json.at(1) == 2); @@ -117,8 +117,8 @@ TEST_CASE("Custom iterator") CHECK(std::is_same::value); CHECK(std::is_same::value); - MyIterator begin{raw_data}; - MyIterator end{raw_data + strlen(raw_data)}; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + const MyIterator begin{raw_data}; + const MyIterator end{raw_data + strlen(raw_data)}; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) json as_json = json::parse(begin, end); CHECK(as_json.at(0) == 1);