🚨 fix warnings

This commit is contained in:
Niels Lohmann 2021-01-28 17:10:52 +01:00
parent 0dcb01bc47
commit bfd4c7c739
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
15 changed files with 57 additions and 36 deletions

View File

@ -5,6 +5,7 @@ Checks: '*,
-cppcoreguidelines-avoid-non-const-global-variables, -cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-macro-usage, -cppcoreguidelines-macro-usage,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay, -cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-reinterpret-cast, -cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-pro-type-union-access, -cppcoreguidelines-pro-type-union-access,
-fuchsia-default-arguments-calls, -fuchsia-default-arguments-calls,

View File

@ -618,7 +618,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
JSON_ASSERT(p1 > 0); JSON_ASSERT(p1 > 0);
std::uint32_t pow10; std::uint32_t pow10{};
const int k = find_largest_pow10(p1, pow10); const int k = find_largest_pow10(p1, pow10);
// 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)

View File

@ -180,7 +180,7 @@ class json_pointer
@since version 3.6.0 @since version 3.6.0
*/ */
friend json_pointer operator/(const json_pointer& ptr, std::string token) friend json_pointer operator/(const json_pointer& ptr, std::string token) // NOLINT(performance-unnecessary-value-param)
{ {
return json_pointer(ptr) /= std::move(token); return json_pointer(ptr) /= std::move(token);
} }

View File

@ -700,7 +700,7 @@ class serializer
} }
// use a pointer to fill the buffer // use a pointer to fill the buffer
auto* buffer_ptr = number_buffer.begin(); auto buffer_ptr = number_buffer.begin();
const bool is_negative = std::is_same<NumberType, number_integer_t>::value && !(x >= 0); // see issue #755 const bool is_negative = std::is_same<NumberType, number_integer_t>::value && !(x >= 0); // see issue #755
number_unsigned_t abs_value; number_unsigned_t abs_value;

View File

@ -2821,7 +2821,7 @@ class basic_json
static ReferenceType get_ref_impl(ThisType& obj) static ReferenceType get_ref_impl(ThisType& obj)
{ {
// delegate the call to get_ptr<>() // delegate the call to get_ptr<>()
auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>(); auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
if (JSON_HEDLEY_LIKELY(ptr != nullptr)) if (JSON_HEDLEY_LIKELY(ptr != nullptr))
{ {

View File

@ -11834,7 +11834,7 @@ class json_pointer
@since version 3.6.0 @since version 3.6.0
*/ */
friend json_pointer operator/(const json_pointer& ptr, std::string token) friend json_pointer operator/(const json_pointer& ptr, std::string token) // NOLINT(performance-unnecessary-value-param)
{ {
return json_pointer(ptr) /= std::move(token); return json_pointer(ptr) /= std::move(token);
} }
@ -15071,7 +15071,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
JSON_ASSERT(p1 > 0); JSON_ASSERT(p1 > 0);
std::uint32_t pow10; std::uint32_t pow10{};
const int k = find_largest_pow10(p1, pow10); const int k = find_largest_pow10(p1, pow10);
// 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
@ -16248,7 +16248,7 @@ class serializer
} }
// use a pointer to fill the buffer // use a pointer to fill the buffer
auto* buffer_ptr = number_buffer.begin(); auto buffer_ptr = number_buffer.begin();
const bool is_negative = std::is_same<NumberType, number_integer_t>::value && !(x >= 0); // see issue #755 const bool is_negative = std::is_same<NumberType, number_integer_t>::value && !(x >= 0); // see issue #755
number_unsigned_t abs_value; number_unsigned_t abs_value;
@ -19442,7 +19442,7 @@ class basic_json
static ReferenceType get_ref_impl(ThisType& obj) static ReferenceType get_ref_impl(ThisType& obj)
{ {
// delegate the call to get_ptr<>() // delegate the call to get_ptr<>()
auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>(); auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
if (JSON_HEDLEY_LIKELY(ptr != nullptr)) if (JSON_HEDLEY_LIKELY(ptr != nullptr))
{ {

View File

@ -738,7 +738,7 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
{ {
return false; return false;
} }

View File

@ -109,7 +109,7 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
{ {
return false; return false;
} }

View File

@ -1691,7 +1691,7 @@ TEST_CASE("parser class")
SECTION("from array") SECTION("from array")
{ {
uint8_t v[] = {'t', 'r', 'u', 'e'}; uint8_t v[] = {'t', 'r', 'u', 'e'}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
json j; json j;
json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j); json::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j);
CHECK(j == json(true)); CHECK(j == json(true));

View File

@ -436,7 +436,7 @@ TEST_CASE("constructors")
SECTION("char[]") SECTION("char[]")
{ {
char s[] {"Hello world"}; char s[] {"Hello world"}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
json j(s); json j(s);
CHECK(j.type() == json::value_t::string); CHECK(j.type() == json::value_t::string);
CHECK(j == j_reference); CHECK(j == j_reference);
@ -1115,24 +1115,29 @@ TEST_CASE("constructors")
{ {
SECTION("string") SECTION("string")
{ {
// This should break through any short string optimization in std::string
std::string source(1024, '!');
const char* source_addr = source.data();
SECTION("constructor with implicit types (array)") SECTION("constructor with implicit types (array)")
{ {
// This should break through any short string optimization in std::string
std::string source(1024, '!');
const char* source_addr = source.data();
json j = {std::move(source)}; json j = {std::move(source)};
CHECK(j[0].get_ref<std::string const&>().data() == source_addr); CHECK(j[0].get_ref<std::string const&>().data() == source_addr);
} }
SECTION("constructor with implicit types (object)") SECTION("constructor with implicit types (object)")
{ {
// This should break through any short string optimization in std::string
std::string source(1024, '!');
const char* source_addr = source.data();
json j = {{"key", std::move(source)}}; json j = {{"key", std::move(source)}};
CHECK(j["key"].get_ref<std::string const&>().data() == source_addr); CHECK(j["key"].get_ref<std::string const&>().data() == source_addr);
} }
SECTION("constructor with implicit types (object key)") SECTION("constructor with implicit types (object key)")
{ {
// This should break through any short string optimization in std::string
std::string source(1024, '!');
const char* source_addr = source.data();
json j = {{std::move(source), 42}}; json j = {{std::move(source), 42}};
CHECK(j.get_ref<json::object_t&>().begin()->first.data() == source_addr); CHECK(j.get_ref<json::object_t&>().begin()->first.data() == source_addr);
} }
@ -1140,29 +1145,34 @@ TEST_CASE("constructors")
SECTION("array") SECTION("array")
{ {
json::array_t source = {1, 2, 3};
const json* source_addr = source.data();
SECTION("constructor with implicit types (array)") SECTION("constructor with implicit types (array)")
{ {
json::array_t source = {1, 2, 3};
const json* source_addr = source.data();
json j {std::move(source)}; json j {std::move(source)};
CHECK(j[0].get_ref<json::array_t const&>().data() == source_addr); CHECK(j[0].get_ref<json::array_t const&>().data() == source_addr);
} }
SECTION("constructor with implicit types (object)") SECTION("constructor with implicit types (object)")
{ {
json::array_t source = {1, 2, 3};
const json* source_addr = source.data();
json j {{"key", std::move(source)}}; json j {{"key", std::move(source)}};
CHECK(j["key"].get_ref<json::array_t const&>().data() == source_addr); CHECK(j["key"].get_ref<json::array_t const&>().data() == source_addr);
} }
SECTION("assignment with implicit types (array)") SECTION("assignment with implicit types (array)")
{ {
json::array_t source = {1, 2, 3};
const json* source_addr = source.data();
json j = {std::move(source)}; json j = {std::move(source)};
CHECK(j[0].get_ref<json::array_t const&>().data() == source_addr); CHECK(j[0].get_ref<json::array_t const&>().data() == source_addr);
} }
SECTION("assignment with implicit types (object)") SECTION("assignment with implicit types (object)")
{ {
json::array_t source = {1, 2, 3};
const json* source_addr = source.data();
json j = {{"key", std::move(source)}}; json j = {{"key", std::move(source)}};
CHECK(j["key"].get_ref<json::array_t const&>().data() == source_addr); CHECK(j["key"].get_ref<json::array_t const&>().data() == source_addr);
} }
@ -1170,29 +1180,34 @@ TEST_CASE("constructors")
SECTION("object") SECTION("object")
{ {
json::object_t source = {{"hello", "world"}};
const json* source_addr = &source.at("hello");
SECTION("constructor with implicit types (array)") SECTION("constructor with implicit types (array)")
{ {
json::object_t source = {{"hello", "world"}};
const json* source_addr = &source.at("hello");
json j {std::move(source)}; json j {std::move(source)};
CHECK(&(j[0].get_ref<json::object_t const&>().at("hello")) == source_addr); CHECK(&(j[0].get_ref<json::object_t const&>().at("hello")) == source_addr);
} }
SECTION("constructor with implicit types (object)") SECTION("constructor with implicit types (object)")
{ {
json::object_t source = {{"hello", "world"}};
const json* source_addr = &source.at("hello");
json j {{"key", std::move(source)}}; json j {{"key", std::move(source)}};
CHECK(&(j["key"].get_ref<json::object_t const&>().at("hello")) == source_addr); CHECK(&(j["key"].get_ref<json::object_t const&>().at("hello")) == source_addr);
} }
SECTION("assignment with implicit types (array)") SECTION("assignment with implicit types (array)")
{ {
json::object_t source = {{"hello", "world"}};
const json* source_addr = &source.at("hello");
json j = {std::move(source)}; json j = {std::move(source)};
CHECK(&(j[0].get_ref<json::object_t const&>().at("hello")) == source_addr); CHECK(&(j[0].get_ref<json::object_t const&>().at("hello")) == source_addr);
} }
SECTION("assignment with implicit types (object)") SECTION("assignment with implicit types (object)")
{ {
json::object_t source = {{"hello", "world"}};
const json* source_addr = &source.at("hello");
json j = {{"key", std::move(source)}}; json j = {{"key", std::move(source)}};
CHECK(&(j["key"].get_ref<json::object_t const&>().at("hello")) == source_addr); CHECK(&(j["key"].get_ref<json::object_t const&>().at("hello")) == source_addr);
} }
@ -1200,29 +1215,34 @@ TEST_CASE("constructors")
SECTION("json") SECTION("json")
{ {
json source {1, 2, 3};
const json* source_addr = &source[0];
SECTION("constructor with implicit types (array)") SECTION("constructor with implicit types (array)")
{ {
json source {1, 2, 3};
const json* source_addr = &source[0];
json j {std::move(source), {}}; json j {std::move(source), {}};
CHECK(&j[0][0] == source_addr); CHECK(&j[0][0] == source_addr);
} }
SECTION("constructor with implicit types (object)") SECTION("constructor with implicit types (object)")
{ {
json source {1, 2, 3};
const json* source_addr = &source[0];
json j {{"key", std::move(source)}}; json j {{"key", std::move(source)}};
CHECK(&j["key"][0] == source_addr); CHECK(&j["key"][0] == source_addr);
} }
SECTION("assignment with implicit types (array)") SECTION("assignment with implicit types (array)")
{ {
json source {1, 2, 3};
const json* source_addr = &source[0];
json j = {std::move(source), {}}; json j = {std::move(source), {}};
CHECK(&j[0][0] == source_addr); CHECK(&j[0][0] == source_addr);
} }
SECTION("assignment with implicit types (object)") SECTION("assignment with implicit types (object)")
{ {
json source {1, 2, 3};
const json* source_addr = &source[0];
json j = {{"key", std::move(source)}}; json j = {{"key", std::move(source)}};
CHECK(&j["key"][0] == source_addr); CHECK(&j["key"][0] == source_addr);
} }

View File

@ -282,8 +282,8 @@ TEST_CASE("value conversion")
SECTION("built-in arrays") SECTION("built-in arrays")
{ {
const char str[] = "a string"; const char str[] = "a string"; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
const int nbs[] = {0, 1, 2}; const int nbs[] = {0, 1, 2}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
json j2 = nbs; json j2 = nbs;
json j3 = str; json j3 = str;
@ -387,8 +387,8 @@ TEST_CASE("value conversion")
SECTION("built-in arrays") SECTION("built-in arrays")
{ {
const int nbs[] = {0, 1, 2}; const int nbs[] = {0, 1, 2}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
int nbs2[] = {0, 0, 0}; int nbs2[] = {0, 0, 0}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
json j2 = nbs; json j2 = nbs;
j2.get_to(nbs2); j2.get_to(nbs2);

View File

@ -107,7 +107,7 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
{ {
return false; return false;
} }

View File

@ -318,7 +318,7 @@ TEST_CASE("regression tests 2")
SECTION("test case in issue #1445") SECTION("test case in issue #1445")
{ {
nlohmann::json dump_test; nlohmann::json dump_test;
const int data[] = const std::array<int, 108> data =
{ {
109, 108, 103, 125, -122, -53, 115, 109, 108, 103, 125, -122, -53, 115,
18, 3, 0, 102, 19, 1, 15, 18, 3, 0, 102, 19, 1, 15,
@ -395,7 +395,7 @@ TEST_CASE("regression tests 2")
SECTION("string array") SECTION("string array")
{ {
const char input[] = { 'B', 0x00 }; const std::array<char, 2> input = { 'B', 0x00 };
json cbor = json::from_cbor(input, true, false); json cbor = json::from_cbor(input, true, false);
CHECK(cbor.is_discarded()); CHECK(cbor.is_discarded());
} }

View File

@ -217,12 +217,12 @@ TEST_CASE("digit gen")
CAPTURE(digits) CAPTURE(digits)
CAPTURE(expected_exponent) CAPTURE(expected_exponent)
char buf[32]; std::array<char, 32> buf{};
int len = 0; int len = 0;
int exponent = 0; int exponent = 0;
nlohmann::detail::dtoa_impl::grisu2(buf, len, exponent, number); nlohmann::detail::dtoa_impl::grisu2(buf.data(), len, exponent, number);
CHECK(digits == std::string(buf, buf + len)); CHECK(digits == std::string(buf.data(), buf.data() + len));
CHECK(expected_exponent == exponent); CHECK(expected_exponent == exponent);
}; };

View File

@ -106,7 +106,7 @@ class SaxCountdown
return events_left-- > 0; return events_left-- > 0;
} }
bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const json::exception& /*unused*/) // NOLINT(readability-convert-member-functions-to-static)
{ {
return false; return false;
} }