🚨 fix Clang-Tidy warnings

This commit is contained in:
Niels Lohmann 2022-09-10 16:08:56 +02:00
parent 1fb18d6e10
commit 42df2cd3a4
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
15 changed files with 147 additions and 147 deletions

View File

@ -330,7 +330,7 @@ class binary_reader
{
std::array<char, 3> cr{{}};
static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(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,

View File

@ -9408,7 +9408,7 @@ class binary_reader
{
std::array<char, 3> cr{{}};
static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(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,

View File

@ -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<nlohmann::json>(ns) == expected);
}

View File

@ -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<nlohmann::json>(ns) == expected);
}

View File

@ -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<nlohmann::json>(ns) == expected);
}

View File

@ -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));

View File

@ -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());
}

View File

@ -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<json::string_t, json>")
{
std::map<json::string_t, json> o =
const std::map<json::string_t, json> o =
j.get<std::map<json::string_t, json>>();
CHECK(json(o) == j);
}
SECTION("std::multimap<json::string_t, json>")
{
std::multimap<json::string_t, json> o =
const std::multimap<json::string_t, json> o =
j.get<std::multimap<json::string_t, json>>();
CHECK(json(o) == j);
}
SECTION("std::unordered_map<json::string_t, json>")
{
std::unordered_map<json::string_t, json> o =
const std::unordered_map<json::string_t, json> o =
j.get<std::unordered_map<json::string_t, json>>();
CHECK(json(o) == j);
}
SECTION("std::unordered_multimap<json::string_t, json>")
{
std::unordered_multimap<json::string_t, json> o =
const std::unordered_multimap<json::string_t, json> o =
j.get<std::unordered_multimap<json::string_t, json>>();
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<json::string_t, json>")
{
std::map<json::string_t, json> o = j;
const std::map<json::string_t, json> o = j;
CHECK(json(o) == j);
}
SECTION("std::multimap<json::string_t, json>")
{
std::multimap<json::string_t, json> o = j;
const std::multimap<json::string_t, json> o = j;
CHECK(json(o) == j);
}
SECTION("std::unordered_map<json::string_t, json>")
{
std::unordered_map<json::string_t, json> o = j;
const std::unordered_map<json::string_t, json> o = j;
CHECK(json(o) == j);
}
SECTION("std::unordered_multimap<json::string_t, json>")
{
std::unordered_multimap<json::string_t, json> o = j;
const std::unordered_multimap<json::string_t, json> 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<json::array_t>();
const json::array_t a = j.get<json::array_t>();
CHECK(json(a) == j);
}
SECTION("std::list<json>")
{
std::list<json> a = j.get<std::list<json>>();
const std::list<json> a = j.get<std::list<json>>();
CHECK(json(a) == j);
}
SECTION("std::forward_list<json>")
{
std::forward_list<json> a = j.get<std::forward_list<json>>();
const std::forward_list<json> a = j.get<std::forward_list<json>>();
CHECK(json(a) == j);
CHECK_THROWS_WITH_AS(
@ -226,7 +226,7 @@ TEST_CASE("value conversion")
SECTION("std::vector<json>")
{
std::vector<json> a = j.get<std::vector<json>>();
const std::vector<json> a = j.get<std::vector<json>>();
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<std::vector<int>>();
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<std::vector<int>>();
auto s = j3.get<std::string>();
@ -260,7 +260,7 @@ TEST_CASE("value conversion")
SECTION("std::deque<json>")
{
std::deque<json> a = j.get<std::deque<json>>();
const std::deque<json> a = j.get<std::deque<json>>();
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<json>")
{
std::list<json> a = j;
const std::list<json> a = j;
CHECK(json(a) == j);
}
SECTION("std::forward_list<json>")
{
std::forward_list<json> a = j;
const std::forward_list<json> a = j;
CHECK(json(a) == j);
}
SECTION("std::vector<json>")
{
std::vector<json> a = j;
const std::vector<json> a = j;
CHECK(json(a) == j);
}
SECTION("std::deque<json>")
{
std::deque<json> a = j;
const std::deque<json> 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<json::string_t>();
const json::string_t s = j.get<json::string_t>();
CHECK(json(s) == j);
}
SECTION("std::string")
{
std::string s = j.get<std::string>();
const std::string s = j.get<std::string>();
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<std::nullptr_t>();
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<bool>();
const bool b = j.get<bool>();
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<int>();
const int n = j.get<int>();
CHECK(json(n) == j);
}
@ -662,7 +662,7 @@ TEST_CASE("value conversion")
SECTION("long")
{
long n = j.get<long>();
const long n = j.get<long>();
CHECK(json(n) == j);
}

View File

@ -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},
{

View File

@ -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<char>(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<int64_t>::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<for_1647>();
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<char, 2> 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<unsigned char, 23> 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<uint8_t> data =
const std::vector<uint8_t> 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 vector<pair>does 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<std::pair<std::string, int64_t>> intData = {std::make_pair("aaaa", 11),
std::make_pair("bbb", 222)
};
const std::vector<std::pair<std::string, int64_t>> 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<std::array<NonDefaultConstructible, 2>>();
CHECK(arr[0].x == 7);
CHECK(arr[1].x == 4);
}
{
json j = 7;
const json j = 7;
CHECK_THROWS_AS((j.get<std::array<NonDefaultConstructible, 1>>()), 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<std::pair<NonDefaultConstructible, NonDefaultConstructible>>();
CHECK(p.first.x == 3);
CHECK(p.second.x == 8);
}
{
json j = {4, 1};
const json j = {4, 1};
auto p = j.get<std::pair<int, NonDefaultConstructible>>();
CHECK(p.first == 4);
CHECK(p.second.x == 1);
}
{
json j = {6, 7};
const json j = {6, 7};
auto p = j.get<std::pair<NonDefaultConstructible, int>>();
CHECK(p.first.x == 6);
CHECK(p.second == 7);
}
{
json j = 7;
const json j = 7;
CHECK_THROWS_AS((j.get<std::pair<NonDefaultConstructible, int>>()), 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<std::tuple<NonDefaultConstructible>>();
CHECK(std::get<0>(t).x == 9);
}
{
json j = {9, 8, 7};
const json j = {9, 8, 7};
auto t = j.get<std::tuple<NonDefaultConstructible, int, NonDefaultConstructible>>();
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<std::tuple<NonDefaultConstructible>>()), 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<nlohmann::detail::std_fs::path>();
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<char>})
@ -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));

View File

@ -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<T>::min)();
json j = minimum;
const json j = minimum;
CHECK(j.dump() == std::to_string(minimum));
}
SECTION("maximum")
{
constexpr auto maximum = (std::numeric_limits<T>::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")
{

View File

@ -302,7 +302,7 @@ TEST_CASE("compliance tests from nativejson-benchmark")
(std::istreambuf_iterator<char>()) );
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<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/1.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> 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<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/2.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> 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<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/3.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> 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<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/4.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> 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<std::FILE, decltype(&std::fclose)> f(std::fopen(TEST_DATA_DIRECTORY "/json.org/5.json", "r"), &std::fclose);
const std::unique_ptr<std::FILE, decltype(&std::fclose)> 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);
}
}

View File

@ -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<uint64_t>(e + kExponentBias);
const uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0)
? 0
: static_cast<uint64_t>(e + kExponentBias);
uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize);
const uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize);
return reinterpret_bits<float>(static_cast<uint32_t>(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<uint64_t>(e + kExponentBias);
const uint64_t biased_exponent = (e == kDenormalExponent && (f & kHiddenBit) == 0)
? 0
: static_cast<uint64_t>(e + kExponentBias);
uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize);
const uint64_t bits = (f & kSignificandMask) | (biased_exponent << kPhysicalSignificandSize);
return reinterpret_bits<double>(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);
};

View File

@ -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<std::string>() == s);
}
else

View File

@ -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<MyIterator::reference, const char&>::value);
CHECK(std::is_same<MyIterator::iterator_category, std::input_iterator_tag>::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);