🚨 fix warnings from Clang-Tidy

This commit is contained in:
Niels Lohmann 2021-01-27 22:48:04 +01:00
parent 0a27d1cf59
commit cb28e76aa4
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
17 changed files with 120 additions and 119 deletions

View File

@ -152,7 +152,7 @@ class alt_string
private: private:
std::string str_impl {}; std::string str_impl {};
friend bool ::operator<(const char*, const alt_string&); friend bool ::operator<(const char* /*op1*/, const alt_string& /*op2*/);
}; };
void int_to_string(alt_string& target, std::size_t value) void int_to_string(alt_string& target, std::size_t value)
@ -233,24 +233,24 @@ TEST_CASE("alternative string type")
SECTION("parse") SECTION("parse")
{ {
auto doc = alt_json::parse("{\"foo\": \"bar\"}"); auto doc = alt_json::parse(R"({"foo": "bar"})");
alt_string dump = doc.dump(); alt_string dump = doc.dump();
CHECK(dump == R"({"foo":"bar"})"); CHECK(dump == R"({"foo":"bar"})");
} }
SECTION("items") SECTION("items")
{ {
auto doc = alt_json::parse("{\"foo\": \"bar\"}"); auto doc = alt_json::parse(R"({"foo": "bar"})");
for ( auto item : doc.items() ) for (const auto& item : doc.items())
{ {
CHECK( item.key() == "foo" ); CHECK(item.key() == "foo");
CHECK( item.value() == "bar" ); CHECK(item.value() == "bar");
} }
auto doc_array = alt_json::parse("[\"foo\", \"bar\"]"); auto doc_array = alt_json::parse(R"(["foo", "bar"])");
for ( auto item : doc_array.items() ) for (const auto& item : doc_array.items())
{ {
if (item.key() == "0" ) if (item.key() == "0" )
{ {
@ -258,11 +258,11 @@ TEST_CASE("alternative string type")
} }
else if (item.key() == "1" ) else if (item.key() == "1" )
{ {
CHECK( item.value() == "bar" ); CHECK(item.value() == "bar");
} }
else else
{ {
CHECK( false ); CHECK(false);
} }
} }
} }

View File

@ -683,42 +683,42 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool boolean(bool) bool boolean(bool /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_integer(json::number_integer_t) bool number_integer(json::number_integer_t /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_unsigned(json::number_unsigned_t) bool number_unsigned(json::number_unsigned_t /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_float(json::number_float_t, const std::string&) bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&) bool string(std::string& /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool binary(std::vector<std::uint8_t>&) bool binary(std::vector<std::uint8_t>& /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_object(std::size_t) bool start_object(std::size_t /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&) bool key(std::string& /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
@ -728,7 +728,7 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool start_array(std::size_t) bool start_array(std::size_t /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
@ -738,7 +738,7 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t, const std::string&, const json::exception&) bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/)
{ {
return false; return false;
} }
@ -746,7 +746,7 @@ class SaxCountdown
private: private:
int events_left = 0; int events_left = 0;
}; };
} } // namespace
TEST_CASE("Incomplete BSON Input") TEST_CASE("Incomplete BSON Input")
{ {

View File

@ -41,7 +41,7 @@ bool f(A a, B b, U u = U())
{ {
return u(a, b); return u(a, b);
} }
} } // namespace
TEST_CASE("lexicographical comparison operators") TEST_CASE("lexicographical comparison operators")
{ {
@ -143,10 +143,10 @@ TEST_CASE("lexicographical comparison operators")
// comparison with discarded elements // comparison with discarded elements
json j_discarded(json::value_t::discarded); json j_discarded(json::value_t::discarded);
for (size_t i = 0; i < j_values.size(); ++i) for (const auto& v : j_values)
{ {
CHECK( (j_values[i] == j_discarded) == false); CHECK( (v == j_discarded) == false);
CHECK( (j_discarded == j_values[i]) == false); CHECK( (j_discarded == v) == false);
CHECK( (j_discarded == j_discarded) == false); CHECK( (j_discarded == j_discarded) == false);
} }

View File

@ -188,19 +188,19 @@ TEST_CASE("other constructors and destructor")
{ {
SECTION("object") SECTION("object")
{ {
auto j = new json {{"foo", 1}, {"bar", false}}; auto* j = new json {{"foo", 1}, {"bar", false}};
delete j; delete j;
} }
SECTION("array") SECTION("array")
{ {
auto j = new json {"foo", 1, 1u, false, 23.42}; auto* j = new json {"foo", 1, 1u, false, 23.42};
delete j; delete j;
} }
SECTION("string") SECTION("string")
{ {
auto j = new json("Hello world"); auto* j = new json("Hello world");
delete j; delete j;
} }
} }

View File

@ -37,7 +37,7 @@ using nlohmann::json;
namespace namespace
{ {
void check_escaped(const char* original, const char* escaped = "", const bool ensure_ascii = false); void check_escaped(const char* original, const char* escaped = "", bool ensure_ascii = false);
void check_escaped(const char* original, const char* escaped, const bool ensure_ascii) void check_escaped(const char* original, const char* escaped, const bool ensure_ascii)
{ {
std::stringstream ss; std::stringstream ss;
@ -45,7 +45,7 @@ void check_escaped(const char* original, const char* escaped, const bool ensure_
s.dump_escaped(original, ensure_ascii); s.dump_escaped(original, ensure_ascii);
CHECK(ss.str() == escaped); CHECK(ss.str() == escaped);
} }
} } // namespace
TEST_CASE("convenience functions") TEST_CASE("convenience functions")
{ {

View File

@ -1258,7 +1258,7 @@ TEST_CASE("JSON patch")
SECTION("Tests from github.com/json-patch/json-patch-tests") SECTION("Tests from github.com/json-patch/json-patch-tests")
{ {
for (auto filename : for (const auto* filename :
{ {
TEST_DATA_DIRECTORY "/json-patch-tests/spec_tests.json", TEST_DATA_DIRECTORY "/json-patch-tests/spec_tests.json",
TEST_DATA_DIRECTORY "/json-patch-tests/tests.json" TEST_DATA_DIRECTORY "/json-patch-tests/tests.json"

View File

@ -527,7 +527,7 @@ TEST_CASE("JSON pointers")
SECTION("string representation") SECTION("string representation")
{ {
for (auto ptr : for (const auto* ptr :
{"", "/foo", "/foo/0", "/", "/a~1b", "/c%d", "/e^f", "/g|h", "/i\\j", "/k\"l", "/ ", "/m~0n" {"", "/foo", "/foo/0", "/", "/a~1b", "/c%d", "/e^f", "/g|h", "/i\\j", "/k\"l", "/ ", "/m~0n"
}) })
{ {

View File

@ -52,42 +52,42 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool boolean(bool) bool boolean(bool /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_integer(json::number_integer_t) bool number_integer(json::number_integer_t /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_unsigned(json::number_unsigned_t) bool number_unsigned(json::number_unsigned_t /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool number_float(json::number_float_t, const std::string&) bool number_float(json::number_float_t /*unused*/, const std::string& /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool string(std::string&) bool string(std::string& /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool binary(std::vector<std::uint8_t>&) bool binary(std::vector<std::uint8_t>& /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool start_object(std::size_t) bool start_object(std::size_t /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
bool key(std::string&) bool key(std::string& /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
@ -97,7 +97,7 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool start_array(std::size_t) bool start_array(std::size_t /*unused*/)
{ {
return events_left-- > 0; return events_left-- > 0;
} }
@ -107,7 +107,7 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t, const std::string&, const json::exception&) bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/)
{ {
return false; return false;
} }
@ -258,7 +258,7 @@ TEST_CASE("MessagePack")
// check individual bytes // check individual bytes
CHECK(result[0] == 0xcc); CHECK(result[0] == 0xcc);
uint8_t restored = static_cast<uint8_t>(result[1]); auto restored = static_cast<uint8_t>(result[1]);
CHECK(restored == i); CHECK(restored == i);
// roundtrip // roundtrip
@ -293,7 +293,7 @@ TEST_CASE("MessagePack")
// check individual bytes // check individual bytes
CHECK(result[0] == 0xcd); CHECK(result[0] == 0xcd);
uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2])); auto restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
CHECK(restored == i); CHECK(restored == i);
// roundtrip // roundtrip
@ -436,7 +436,7 @@ TEST_CASE("MessagePack")
const auto result = json::to_msgpack(j); const auto result = json::to_msgpack(j);
CHECK(result == expected); CHECK(result == expected);
int16_t restored = static_cast<int16_t>((result[1] << 8) + result[2]); auto restored = static_cast<int16_t>((result[1] << 8) + result[2]);
CHECK(restored == -9263); CHECK(restored == -9263);
// roundtrip // roundtrip
@ -469,7 +469,7 @@ TEST_CASE("MessagePack")
// check individual bytes // check individual bytes
CHECK(result[0] == 0xd1); CHECK(result[0] == 0xd1);
int16_t restored = static_cast<int16_t>((result[1] << 8) + result[2]); auto restored = static_cast<int16_t>((result[1] << 8) + result[2]);
CHECK(restored == i); CHECK(restored == i);
// roundtrip // roundtrip
@ -630,7 +630,7 @@ TEST_CASE("MessagePack")
// check individual bytes // check individual bytes
CHECK(result[0] == 0xcc); CHECK(result[0] == 0xcc);
uint8_t restored = static_cast<uint8_t>(result[1]); auto restored = static_cast<uint8_t>(result[1]);
CHECK(restored == i); CHECK(restored == i);
// roundtrip // roundtrip
@ -664,7 +664,7 @@ TEST_CASE("MessagePack")
// check individual bytes // check individual bytes
CHECK(result[0] == 0xcd); CHECK(result[0] == 0xcd);
uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2])); auto restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
CHECK(restored == i); CHECK(restored == i);
// roundtrip // roundtrip
@ -1088,7 +1088,7 @@ TEST_CASE("MessagePack")
SECTION("{\"a\": {\"b\": {\"c\": {}}}}") SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
{ {
json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}"); json j = json::parse(R"({"a": {"b": {"c": {}}}})");
std::vector<uint8_t> expected = std::vector<uint8_t> expected =
{ {
0x81, 0xa1, 0x61, 0x81, 0xa1, 0x62, 0x81, 0xa1, 0x63, 0x80 0x81, 0xa1, 0x61, 0x81, 0xa1, 0x62, 0x81, 0xa1, 0x63, 0x80
@ -1870,7 +1870,7 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip())
// parse MessagePack file // parse MessagePack file
auto packed = utils::read_binary_file(filename + ".msgpack"); auto packed = utils::read_binary_file(filename + ".msgpack");
if (!exclude_packed.count(filename)) if (exclude_packed.count(filename) == 0u)
{ {
{ {
INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>"); INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");

View File

@ -49,6 +49,7 @@ TEST_CASE("ordered_map")
std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}}; std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
ordered_map<std::string, std::string> om(m.begin(), m.end()); ordered_map<std::string, std::string> om(m.begin(), m.end());
const auto com = om; const auto com = om;
om.clear(); // silence a warning by forbidding having "const auto& com = om;"
CHECK(com.size() == 3); CHECK(com.size() == 3);
} }
} }

View File

@ -52,7 +52,7 @@ TEST_CASE("README" * doctest::skip())
{ {
{ {
// redirect std::cout for the README file // redirect std::cout for the README file
auto old_cout_buffer = std::cout.rdbuf(); auto* old_cout_buffer = std::cout.rdbuf();
std::ostringstream new_stream; std::ostringstream new_stream;
std::cout.rdbuf(new_stream.rdbuf()); std::cout.rdbuf(new_stream.rdbuf());
{ {

View File

@ -56,11 +56,11 @@ TEST_CASE("reference access")
json value = {{"one", 1}, {"two", 2}}; json value = {{"one", 1}, {"two", 2}};
// check if references are returned correctly // check if references are returned correctly
test_type& p1 = value.get_ref<test_type&>(); auto& p1 = value.get_ref<test_type&>();
CHECK(&p1 == value.get_ptr<test_type*>()); CHECK(&p1 == value.get_ptr<test_type*>());
CHECK(p1 == value.get<test_type>()); CHECK(p1 == value.get<test_type>());
const test_type& p2 = value.get_ref<const test_type&>(); const auto& p2 = value.get_ref<const test_type&>();
CHECK(&p2 == value.get_ptr<const test_type*>()); CHECK(&p2 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get<test_type>()); CHECK(p2 == value.get<test_type>());
@ -95,7 +95,7 @@ TEST_CASE("reference access")
// test_type& p1 = value.get_ref<test_type&>(); // test_type& p1 = value.get_ref<test_type&>();
// check if references are returned correctly // check if references are returned correctly
const test_type& p2 = value.get_ref<const test_type&>(); const auto& p2 = value.get_ref<const test_type&>();
CHECK(&p2 == value.get_ptr<const test_type*>()); CHECK(&p2 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get<test_type>()); CHECK(p2 == value.get<test_type>());
} }
@ -106,11 +106,11 @@ TEST_CASE("reference access")
json value = {1, 2, 3, 4}; json value = {1, 2, 3, 4};
// check if references are returned correctly // check if references are returned correctly
test_type& p1 = value.get_ref<test_type&>(); auto& p1 = value.get_ref<test_type&>();
CHECK(&p1 == value.get_ptr<test_type*>()); CHECK(&p1 == value.get_ptr<test_type*>());
CHECK(p1 == value.get<test_type>()); CHECK(p1 == value.get<test_type>());
const test_type& p2 = value.get_ref<const test_type&>(); const auto& p2 = value.get_ref<const test_type&>();
CHECK(&p2 == value.get_ptr<const test_type*>()); CHECK(&p2 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get<test_type>()); CHECK(p2 == value.get<test_type>());
@ -142,11 +142,11 @@ TEST_CASE("reference access")
json value = "hello"; json value = "hello";
// check if references are returned correctly // check if references are returned correctly
test_type& p1 = value.get_ref<test_type&>(); auto& p1 = value.get_ref<test_type&>();
CHECK(&p1 == value.get_ptr<test_type*>()); CHECK(&p1 == value.get_ptr<test_type*>());
CHECK(p1 == value.get<test_type>()); CHECK(p1 == value.get<test_type>());
const test_type& p2 = value.get_ref<const test_type&>(); const auto& p2 = value.get_ref<const test_type&>();
CHECK(&p2 == value.get_ptr<const test_type*>()); CHECK(&p2 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get<test_type>()); CHECK(p2 == value.get<test_type>());
@ -178,11 +178,11 @@ TEST_CASE("reference access")
json value = false; json value = false;
// check if references are returned correctly // check if references are returned correctly
test_type& p1 = value.get_ref<test_type&>(); auto& p1 = value.get_ref<test_type&>();
CHECK(&p1 == value.get_ptr<test_type*>()); CHECK(&p1 == value.get_ptr<test_type*>());
CHECK(p1 == value.get<test_type>()); CHECK(p1 == value.get<test_type>());
const test_type& p2 = value.get_ref<const test_type&>(); const auto& p2 = value.get_ref<const test_type&>();
CHECK(&p2 == value.get_ptr<const test_type*>()); CHECK(&p2 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get<test_type>()); CHECK(p2 == value.get<test_type>());
@ -214,11 +214,11 @@ TEST_CASE("reference access")
json value = -23; json value = -23;
// check if references are returned correctly // check if references are returned correctly
test_type& p1 = value.get_ref<test_type&>(); auto& p1 = value.get_ref<test_type&>();
CHECK(&p1 == value.get_ptr<test_type*>()); CHECK(&p1 == value.get_ptr<test_type*>());
CHECK(p1 == value.get<test_type>()); CHECK(p1 == value.get<test_type>());
const test_type& p2 = value.get_ref<const test_type&>(); const auto& p2 = value.get_ref<const test_type&>();
CHECK(&p2 == value.get_ptr<const test_type*>()); CHECK(&p2 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get<test_type>()); CHECK(p2 == value.get<test_type>());
@ -250,11 +250,11 @@ TEST_CASE("reference access")
json value = 23u; json value = 23u;
// check if references are returned correctly // check if references are returned correctly
test_type& p1 = value.get_ref<test_type&>(); auto& p1 = value.get_ref<test_type&>();
CHECK(&p1 == value.get_ptr<test_type*>()); CHECK(&p1 == value.get_ptr<test_type*>());
CHECK(p1 == value.get<test_type>()); CHECK(p1 == value.get<test_type>());
const test_type& p2 = value.get_ref<const test_type&>(); const auto& p2 = value.get_ref<const test_type&>();
CHECK(&p2 == value.get_ptr<const test_type*>()); CHECK(&p2 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get<test_type>()); CHECK(p2 == value.get<test_type>());
@ -286,11 +286,11 @@ TEST_CASE("reference access")
json value = 42.23; json value = 42.23;
// check if references are returned correctly // check if references are returned correctly
test_type& p1 = value.get_ref<test_type&>(); auto& p1 = value.get_ref<test_type&>();
CHECK(&p1 == value.get_ptr<test_type*>()); CHECK(&p1 == value.get_ptr<test_type*>());
CHECK(p1 == value.get<test_type>()); CHECK(p1 == value.get<test_type>());
const test_type& p2 = value.get_ref<const test_type&>(); const auto& p2 = value.get_ref<const test_type&>();
CHECK(&p2 == value.get_ptr<const test_type*>()); CHECK(&p2 == value.get_ptr<const test_type*>());
CHECK(p2 == value.get<test_type>()); CHECK(p2 == value.get<test_type>());

View File

@ -122,7 +122,7 @@ struct adl_serializer<NonDefaultFromJsonStruct>
return {}; return {};
} }
}; };
} } // namespace nlohmann
///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////
// for #1805 // for #1805
@ -139,7 +139,7 @@ TEST_CASE("regression tests 2")
{ {
SECTION("issue #1001 - Fix memory leak during parser callback") SECTION("issue #1001 - Fix memory leak during parser callback")
{ {
auto geojsonExample = R"( const auto* geojsonExample = R"(
{ "type": "FeatureCollection", { "type": "FeatureCollection",
"features": [ "features": [
{ "type": "Feature", { "type": "Feature",
@ -174,7 +174,7 @@ TEST_CASE("regression tests 2")
] ]
})"; })";
json::parser_callback_t cb = [&](int, json::parse_event_t event, json & parsed) json::parser_callback_t cb = [&](int /*level*/, json::parse_event_t event, json & parsed)
{ {
// skip uninteresting events // skip uninteresting events
if (event == json::parse_event_t::value && !parsed.is_primitive()) if (event == json::parse_event_t::value && !parsed.is_primitive())
@ -290,7 +290,7 @@ TEST_CASE("regression tests 2")
json dump_test; json dump_test;
dump_test["1"] = std::string(length, -1); dump_test["1"] = std::string(length, -1);
std::string expected = "{\"1\":\""; std::string expected = R"({"1":")";
for (int i = 0; i < length; ++i) for (int i = 0; i < length; ++i)
{ {
expected += "\\ufffd"; expected += "\\ufffd";
@ -307,7 +307,7 @@ TEST_CASE("regression tests 2")
json dump_test; json dump_test;
dump_test["1"] = std::string(length, -2); dump_test["1"] = std::string(length, -2);
std::string expected = "{\"1\":\""; std::string expected = R"({"1":")";
for (int i = 0; i < length; ++i) for (int i = 0; i < length; ++i)
{ {
expected += "\xEF\xBF\xBD"; expected += "\xEF\xBF\xBD";
@ -340,9 +340,9 @@ TEST_CASE("regression tests 2")
-54, -28, -26 -54, -28, -26
}; };
std::string s; std::string s;
for (unsigned i = 0; i < sizeof(data) / sizeof(int); i++) for (int i : data)
{ {
s += static_cast<char>(data[i]); s += static_cast<char>(i);
} }
dump_test["1"] = s; dump_test["1"] = s;
dump_test.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace); dump_test.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace);

View File

@ -38,7 +38,7 @@ using nlohmann::detail::dtoa_impl::reinterpret_bits;
namespace namespace
{ {
static float make_float(uint32_t sign_bit, uint32_t biased_exponent, uint32_t significand) float make_float(uint32_t sign_bit, uint32_t biased_exponent, uint32_t significand)
{ {
assert(sign_bit == 0 || sign_bit == 1); assert(sign_bit == 0 || sign_bit == 1);
assert(biased_exponent <= 0xFF); assert(biased_exponent <= 0xFF);

View File

@ -34,8 +34,10 @@ using nlohmann::json;
#include <array> #include <array>
#include <map> #include <map>
#include <memory>
#include <string> #include <string>
#include <memory> #include <memory>
#include <utility>
namespace udt namespace udt
{ {
@ -55,40 +57,40 @@ struct age
struct name struct name
{ {
std::string m_val; std::string m_val;
name(const std::string rhs = "") : m_val(rhs) {} name(std::string rhs = "") : m_val(std::move(rhs)) {}
}; };
struct address struct address
{ {
std::string m_val; std::string m_val;
address(const std::string rhs = "") : m_val(rhs) {} address(std::string rhs = "") : m_val(std::move(rhs)) {}
}; };
struct person struct person
{ {
age m_age; age m_age;
name m_name; name m_name;
country m_country; country m_country{};
person() : m_age(), m_name(), m_country() {} person() = default;
person(const age& a, const name& n, const country& c) : m_age(a), m_name(n), m_country(c) {} person(const age& a, name n, const country& c) : m_age(a), m_name(std::move(n)), m_country(c) {}
}; };
struct contact struct contact
{ {
person m_person; person m_person;
address m_address; address m_address;
contact() : m_person(), m_address() {} contact() = default;
contact(const person& p, const address& a) : m_person(p), m_address(a) {} contact(person p, address a) : m_person(std::move(p)), m_address(std::move(a)) {}
}; };
struct contact_book struct contact_book
{ {
name m_book_name; name m_book_name;
std::vector<contact> m_contacts; std::vector<contact> m_contacts;
contact_book() : m_book_name(), m_contacts() {} contact_book() = default;
contact_book(const name& n, const std::vector<contact>& c) : m_book_name(n), m_contacts(c) {} contact_book(name n, std::vector<contact> c) : m_book_name(std::move(n)), m_contacts(std::move(c)) {}
}; };
} } // namespace udt
// to_json methods // to_json methods
namespace udt namespace udt
@ -178,7 +180,7 @@ static bool operator==(const contact_book& lhs, const contact_book& rhs)
return std::tie(lhs.m_book_name, lhs.m_contacts) == return std::tie(lhs.m_book_name, lhs.m_contacts) ==
std::tie(rhs.m_book_name, rhs.m_contacts); std::tie(rhs.m_book_name, rhs.m_contacts);
} }
} } // namespace udt
// from_json methods // from_json methods
namespace udt namespace udt
@ -207,7 +209,7 @@ static void from_json(const BasicJsonType& j, country& c)
}; };
const auto it = m.find(str); const auto it = m.find(str);
// TODO test exceptions // TODO(nlohmann) test exceptions
c = it->second; c = it->second;
} }
@ -235,7 +237,7 @@ static void from_json(const nlohmann::json& j, contact_book& cb)
cb.m_book_name = j["name"].get<name>(); cb.m_book_name = j["name"].get<name>();
cb.m_contacts = j["contacts"].get<std::vector<contact>>(); cb.m_contacts = j["contacts"].get<std::vector<contact>>();
} }
} } // namespace udt
TEST_CASE("basic usage" * doctest::test_suite("udt")) TEST_CASE("basic usage" * doctest::test_suite("udt"))
{ {
@ -345,10 +347,10 @@ namespace udt
struct legacy_type struct legacy_type
{ {
std::string number; std::string number;
legacy_type() : number() {} legacy_type() = default;
legacy_type(const std::string& n) : number(n) {} legacy_type(std::string n) : number(std::move(n)) {}
}; };
} } // namespace udt
namespace nlohmann namespace nlohmann
{ {
@ -393,7 +395,7 @@ struct adl_serializer<udt::legacy_type>
l.number = std::to_string(j.get<int>()); l.number = std::to_string(j.get<int>());
} }
}; };
} } // namespace nlohmann
TEST_CASE("adl_serializer specialization" * doctest::test_suite("udt")) TEST_CASE("adl_serializer specialization" * doctest::test_suite("udt"))
{ {
@ -453,23 +455,23 @@ template <>
struct adl_serializer<std::vector<float>> struct adl_serializer<std::vector<float>>
{ {
using type = std::vector<float>; using type = std::vector<float>;
static void to_json(json& j, const type&) static void to_json(json& j, const type& /*type*/)
{ {
j = "hijacked!"; j = "hijacked!";
} }
static void from_json(const json&, type& opt) static void from_json(const json& /*unnamed*/, type& opt)
{ {
opt = {42.0, 42.0, 42.0}; opt = {42.0, 42.0, 42.0};
} }
// preferred version // preferred version
static type from_json(const json&) static type from_json(const json& /*unnamed*/)
{ {
return {4.0, 5.0, 6.0}; return {4.0, 5.0, 6.0};
} }
}; };
} } // namespace nlohmann
TEST_CASE("even supported types can be specialized" * doctest::test_suite("udt")) TEST_CASE("even supported types can be specialized" * doctest::test_suite("udt"))
{ {
@ -504,13 +506,11 @@ struct adl_serializer<std::unique_ptr<T>>
{ {
return nullptr; return nullptr;
} }
else
{ return std::unique_ptr<T>(new T(j.get<T>()));
return std::unique_ptr<T>(new T(j.get<T>()));
}
} }
}; };
} } // namespace nlohmann
TEST_CASE("Non-copyable types" * doctest::test_suite("udt")) TEST_CASE("Non-copyable types" * doctest::test_suite("udt"))
{ {
@ -566,8 +566,8 @@ struct pod_serializer
std::is_pod<U>::value && std::is_class<U>::value, int >::type = 0 > std::is_pod<U>::value && std::is_class<U>::value, int >::type = 0 >
static void from_json(const BasicJsonType& j, U& t) static void from_json(const BasicJsonType& j, U& t)
{ {
std::uint64_t value; std::uint64_t value = 0;
// TODO The following block is no longer relevant in this serializer, make another one that shows the issue // The following block is no longer relevant in this serializer, make another one that shows the issue
// the problem arises only when one from_json method is defined without any constraint // the problem arises only when one from_json method is defined without any constraint
// //
// Why cannot we simply use: j.get<std::uint64_t>() ? // Why cannot we simply use: j.get<std::uint64_t>() ?
@ -582,7 +582,7 @@ struct pod_serializer
// calling get calls from_json, for now, we cannot do this in custom // calling get calls from_json, for now, we cannot do this in custom
// serializers // serializers
nlohmann::from_json(j, value); nlohmann::from_json(j, value);
auto bytes = static_cast<char*>(static_cast<void*>(&value)); auto* bytes = static_cast<char*>(static_cast<void*>(&value));
std::memcpy(&t, bytes, sizeof(value)); std::memcpy(&t, bytes, sizeof(value));
} }
@ -601,8 +601,8 @@ struct pod_serializer
std::is_pod<U>::value && std::is_class<U>::value, int >::type = 0 > std::is_pod<U>::value && std::is_class<U>::value, int >::type = 0 >
static void to_json(BasicJsonType& j, const T& t) noexcept static void to_json(BasicJsonType& j, const T& t) noexcept
{ {
auto bytes = static_cast< const unsigned char*>(static_cast<const void*>(&t)); const auto* bytes = static_cast< const unsigned char*>(static_cast<const void*>(&t));
std::uint64_t value; std::uint64_t value = 0;
std::memcpy(&value, bytes, sizeof(value)); std::memcpy(&value, bytes, sizeof(value));
nlohmann::to_json(j, value); nlohmann::to_json(j, value);
} }
@ -620,8 +620,8 @@ struct small_pod
struct non_pod struct non_pod
{ {
std::string s; std::string s;
non_pod() : s() {} non_pod() = default;
non_pod(const std::string& S) : s(S) {} non_pod(std::string S) : s(std::move(S)) {}
}; };
template <typename BasicJsonType> template <typename BasicJsonType>
@ -651,7 +651,7 @@ static std::ostream& operator<<(std::ostream& os, small_pod l)
{ {
return os << "begin: " << l.begin << ", middle: " << l.middle << ", end: " << l.end; return os << "begin: " << l.begin << ", middle: " << l.middle << ", end: " << l.end;
} }
} } // namespace udt
TEST_CASE("custom serializer for pods" * doctest::test_suite("udt")) TEST_CASE("custom serializer for pods" * doctest::test_suite("udt"))
{ {
@ -803,7 +803,7 @@ struct is_constructible_patched : std::false_type {};
template <typename T> template <typename T>
struct is_constructible_patched<T, decltype(void(json(std::declval<T>())))> : std::true_type {}; struct is_constructible_patched<T, decltype(void(json(std::declval<T>())))> : std::true_type {};
} } // namespace
TEST_CASE("an incomplete type does not trigger a compiler error in non-evaluated context" * doctest::test_suite("udt")) TEST_CASE("an incomplete type does not trigger a compiler error in non-evaluated context" * doctest::test_suite("udt"))
{ {
@ -822,8 +822,8 @@ class Evil
int m_i = 0; int m_i = 0;
}; };
void from_json(const json&, Evil&) {} void from_json(const json& /*unused*/, Evil& /*unused*/) {}
} } // namespace
TEST_CASE("Issue #924") TEST_CASE("Issue #924")
{ {

View File

@ -39,7 +39,7 @@ namespace persons
class person_with_private_data class person_with_private_data
{ {
private: private:
std::string name = ""; std::string name;
int age = 0; int age = 0;
json metadata = nullptr; json metadata = nullptr;
@ -62,7 +62,7 @@ class person_with_private_data
class person_without_private_data_1 class person_without_private_data_1
{ {
public: public:
std::string name = ""; std::string name;
int age = 0; int age = 0;
json metadata = nullptr; json metadata = nullptr;
@ -84,7 +84,7 @@ class person_without_private_data_1
class person_without_private_data_2 class person_without_private_data_2
{ {
public: public:
std::string name = ""; std::string name;
int age = 0; int age = 0;
json metadata = nullptr; json metadata = nullptr;

View File

@ -168,7 +168,7 @@ void check_utf8string(bool success_expected, int byte1, int byte2 = -1, int byte
CHECK_THROWS_AS(_ = json::parse(json_string), json::parse_error&); CHECK_THROWS_AS(_ = json::parse(json_string), json::parse_error&);
} }
} }
} } // namespace
TEST_CASE("Unicode" * doctest::skip()) TEST_CASE("Unicode" * doctest::skip())
{ {
@ -1159,7 +1159,7 @@ TEST_CASE("Unicode" * doctest::skip())
SECTION("check JSON Pointers") SECTION("check JSON Pointers")
{ {
for (auto s : j) for (const auto& s : j)
{ {
// skip non-string JSON values // skip non-string JSON values
if (!s.is_string()) if (!s.is_string())
@ -1176,7 +1176,7 @@ TEST_CASE("Unicode" * doctest::skip())
} }
// JSON Pointers must begin with "/" // JSON Pointers must begin with "/"
ptr = "/" + ptr; ptr.insert(0, "/");
CHECK_NOTHROW(json::json_pointer("/" + ptr)); CHECK_NOTHROW(json::json_pointer("/" + ptr));
@ -1256,7 +1256,7 @@ void roundtrip(bool success_expected, const std::string& s)
CHECK_THROWS_AS(_ = json::parse(ps), json::parse_error&); CHECK_THROWS_AS(_ = json::parse(ps), json::parse_error&);
} }
} }
} } // namespace
TEST_CASE("Markus Kuhn's UTF-8 decoder capability and stress test") TEST_CASE("Markus Kuhn's UTF-8 decoder capability and stress test")
{ {

View File

@ -51,7 +51,7 @@ bool u32string_is_utf32()
{ {
return (std::u32string(U"💩") == std::u32string(U"\U0001F4A9")); return (std::u32string(U"💩") == std::u32string(U"\U0001F4A9"));
} }
} } // namespace
TEST_CASE("wide strings") TEST_CASE("wide strings")
{ {