diff --git a/.clang-tidy b/.clang-tidy index 63e2902ef..cdbba1fca 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -5,6 +5,7 @@ Checks: '*, -cppcoreguidelines-avoid-non-const-global-variables, -cppcoreguidelines-macro-usage, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, + -cppcoreguidelines-pro-bounds-pointer-arithmetic, -cppcoreguidelines-pro-type-reinterpret-cast, -cppcoreguidelines-pro-type-union-access, -fuchsia-default-arguments-calls, diff --git a/include/nlohmann/detail/conversions/to_chars.hpp b/include/nlohmann/detail/conversions/to_chars.hpp index 49ed0f913..5b098eb8d 100644 --- a/include/nlohmann/detail/conversions/to_chars.hpp +++ b/include/nlohmann/detail/conversions/to_chars.hpp @@ -618,7 +618,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, JSON_ASSERT(p1 > 0); - std::uint32_t pow10; + std::uint32_t pow10{}; const int k = find_largest_pow10(p1, pow10); // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 4209e676a..b89a77a6d 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -180,7 +180,7 @@ class json_pointer @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); } diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index ab155ecb6..54250e38e 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -700,7 +700,7 @@ class serializer } // 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::value && !(x >= 0); // see issue #755 number_unsigned_t abs_value; diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index e5304a779..2eb288ecc 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -2821,7 +2821,7 @@ class basic_json static ReferenceType get_ref_impl(ThisType& obj) { // delegate the call to get_ptr<>() - auto ptr = obj.template get_ptr::type>(); + auto* ptr = obj.template get_ptr::type>(); if (JSON_HEDLEY_LIKELY(ptr != nullptr)) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e82501475..8d6bcb1b1 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -11834,7 +11834,7 @@ class json_pointer @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); } @@ -15071,7 +15071,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, JSON_ASSERT(p1 > 0); - std::uint32_t pow10; + std::uint32_t pow10{}; const int k = find_largest_pow10(p1, pow10); // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) @@ -16248,7 +16248,7 @@ class serializer } // 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::value && !(x >= 0); // see issue #755 number_unsigned_t abs_value; @@ -19442,7 +19442,7 @@ class basic_json static ReferenceType get_ref_impl(ThisType& obj) { // delegate the call to get_ptr<>() - auto ptr = obj.template get_ptr::type>(); + auto* ptr = obj.template get_ptr::type>(); if (JSON_HEDLEY_LIKELY(ptr != nullptr)) { diff --git a/test/src/unit-bson.cpp b/test/src/unit-bson.cpp index e0e020de0..b89bddee6 100644 --- a/test/src/unit-bson.cpp +++ b/test/src/unit-bson.cpp @@ -738,7 +738,7 @@ class SaxCountdown 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; } diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index bfbcf5404..00aafba65 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -109,7 +109,7 @@ class SaxCountdown 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; } diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 6701f5f4e..247d9d5fe 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -1691,7 +1691,7 @@ TEST_CASE("parser class") 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::parser(nlohmann::detail::input_adapter(std::begin(v), std::end(v))).parse(true, j); CHECK(j == json(true)); diff --git a/test/src/unit-constructor1.cpp b/test/src/unit-constructor1.cpp index 6838dd0e5..ba9bf12b7 100644 --- a/test/src/unit-constructor1.cpp +++ b/test/src/unit-constructor1.cpp @@ -436,7 +436,7 @@ TEST_CASE("constructors") 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); CHECK(j.type() == json::value_t::string); CHECK(j == j_reference); @@ -1115,24 +1115,29 @@ TEST_CASE("constructors") { 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)") { + // 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)}; CHECK(j[0].get_ref().data() == source_addr); } 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)}}; CHECK(j["key"].get_ref().data() == source_addr); } 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}}; CHECK(j.get_ref().begin()->first.data() == source_addr); } @@ -1140,29 +1145,34 @@ TEST_CASE("constructors") SECTION("array") { - json::array_t source = {1, 2, 3}; - const json* source_addr = source.data(); - SECTION("constructor with implicit types (array)") { + json::array_t source = {1, 2, 3}; + const json* source_addr = source.data(); json j {std::move(source)}; CHECK(j[0].get_ref().data() == source_addr); } 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)}}; CHECK(j["key"].get_ref().data() == source_addr); } SECTION("assignment with implicit types (array)") { + json::array_t source = {1, 2, 3}; + const json* source_addr = source.data(); json j = {std::move(source)}; CHECK(j[0].get_ref().data() == source_addr); } 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)}}; CHECK(j["key"].get_ref().data() == source_addr); } @@ -1170,29 +1180,34 @@ TEST_CASE("constructors") SECTION("object") { - json::object_t source = {{"hello", "world"}}; - const json* source_addr = &source.at("hello"); - SECTION("constructor with implicit types (array)") { + json::object_t source = {{"hello", "world"}}; + const json* source_addr = &source.at("hello"); json j {std::move(source)}; CHECK(&(j[0].get_ref().at("hello")) == source_addr); } 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)}}; CHECK(&(j["key"].get_ref().at("hello")) == source_addr); } SECTION("assignment with implicit types (array)") { + json::object_t source = {{"hello", "world"}}; + const json* source_addr = &source.at("hello"); json j = {std::move(source)}; CHECK(&(j[0].get_ref().at("hello")) == source_addr); } 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)}}; CHECK(&(j["key"].get_ref().at("hello")) == source_addr); } @@ -1200,29 +1215,34 @@ TEST_CASE("constructors") SECTION("json") { - json source {1, 2, 3}; - const json* source_addr = &source[0]; - SECTION("constructor with implicit types (array)") { + json source {1, 2, 3}; + const json* source_addr = &source[0]; json j {std::move(source), {}}; CHECK(&j[0][0] == source_addr); } SECTION("constructor with implicit types (object)") { + json source {1, 2, 3}; + const json* source_addr = &source[0]; json j {{"key", std::move(source)}}; CHECK(&j["key"][0] == source_addr); } SECTION("assignment with implicit types (array)") { + json source {1, 2, 3}; + const json* source_addr = &source[0]; json j = {std::move(source), {}}; CHECK(&j[0][0] == source_addr); } SECTION("assignment with implicit types (object)") { + json source {1, 2, 3}; + const json* source_addr = &source[0]; json j = {{"key", std::move(source)}}; CHECK(&j["key"][0] == source_addr); } diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index cc6c7d078..6117226e2 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -282,8 +282,8 @@ TEST_CASE("value conversion") SECTION("built-in arrays") { - const char str[] = "a string"; - const int nbs[] = {0, 1, 2}; + 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; @@ -387,8 +387,8 @@ TEST_CASE("value conversion") SECTION("built-in arrays") { - const int nbs[] = {0, 1, 2}; - int nbs2[] = {0, 0, 0}; + 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; j2.get_to(nbs2); diff --git a/test/src/unit-msgpack.cpp b/test/src/unit-msgpack.cpp index 31a22fedc..dde28c4b7 100644 --- a/test/src/unit-msgpack.cpp +++ b/test/src/unit-msgpack.cpp @@ -107,7 +107,7 @@ class SaxCountdown 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; } diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 6dd6e53d5..210bc6787 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -318,7 +318,7 @@ TEST_CASE("regression tests 2") SECTION("test case in issue #1445") { nlohmann::json dump_test; - const int data[] = + const std::array data = { 109, 108, 103, 125, -122, -53, 115, 18, 3, 0, 102, 19, 1, 15, @@ -395,7 +395,7 @@ TEST_CASE("regression tests 2") SECTION("string array") { - const char input[] = { 'B', 0x00 }; + const std::array input = { 'B', 0x00 }; json cbor = json::from_cbor(input, true, false); CHECK(cbor.is_discarded()); } diff --git a/test/src/unit-to_chars.cpp b/test/src/unit-to_chars.cpp index 8ae05cf95..3e8bbec3c 100644 --- a/test/src/unit-to_chars.cpp +++ b/test/src/unit-to_chars.cpp @@ -217,12 +217,12 @@ TEST_CASE("digit gen") CAPTURE(digits) CAPTURE(expected_exponent) - char buf[32]; + std::array buf{}; int len = 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); }; diff --git a/test/src/unit-ubjson.cpp b/test/src/unit-ubjson.cpp index 47fcf2449..0e8837169 100644 --- a/test/src/unit-ubjson.cpp +++ b/test/src/unit-ubjson.cpp @@ -106,7 +106,7 @@ class SaxCountdown 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; }