From 28e20bd9e40387574479f753cd46ff66ed8aa21d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 16 May 2020 14:26:17 +0200 Subject: [PATCH] :rotating_light: fix warnings #2113 --- Makefile | 14 +++++++++----- include/nlohmann/detail/input/binary_reader.hpp | 2 +- .../nlohmann/detail/output/binary_writer.hpp | 6 +++--- single_include/nlohmann/json.hpp | 8 ++++---- test/src/unit-cbor.cpp | 8 ++++---- test/src/unit-noexcept.cpp | 17 ++++++++++++++++- test/src/unit-udt.cpp | 10 +++++++++- test/thirdparty/doctest/doctest.h | 2 +- 8 files changed, 47 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index a2884812bd..08c40d2f34 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,8 @@ doctest: # -Wno-switch-enum -Wno-covered-switch-default: pedantic/contradicting warnings about switches # -Wno-weak-vtables: exception class is defined inline, but has virtual method pedantic_clang: - $(MAKE) json_unit CXX=c++ CXXFLAGS=" \ + rm -fr build_pedantic + CXXFLAGS=" \ -std=c++11 -Wno-c++98-compat -Wno-c++98-compat-pedantic \ -Werror \ -Weverything \ @@ -115,11 +116,13 @@ pedantic_clang: -Wno-padded \ -Wno-range-loop-analysis \ -Wno-switch-enum -Wno-covered-switch-default \ - -Wno-weak-vtables" + -Wno-weak-vtables" cmake -S . -B build_pedantic -GNinja -DCMAKE_BUILD_TYPE=Debug -DJSON_MultipleHeaders=ON + cmake --build build_pedantic # calling GCC with most warnings pedantic_gcc: - $(MAKE) json_unit CXX=/usr/local/bin/g++-9 CXXFLAGS=" \ + rm -fr build_pedantic + CXXFLAGS=" \ -std=c++11 \ -Waddress \ -Waddress-of-packed-member \ @@ -233,7 +236,7 @@ pedantic_gcc: -Wno-system-headers \ -Wno-templates \ -Wno-undef \ - -Wnoexcept \ + -Wno-noexcept \ -Wnoexcept-type \ -Wnon-template-friend \ -Wnon-virtual-dtor \ @@ -340,7 +343,8 @@ pedantic_gcc: -Wvolatile-register-var \ -Wwrite-strings \ -Wzero-as-null-pointer-constant \ - " + " cmake -S . -B build_pedantic -GNinja -DCMAKE_BUILD_TYPE=Debug -DJSON_MultipleHeaders=ON + cmake --build build_pedantic ########################################################################## # benchmarks diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 5f497089e8..a40693a00a 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -32,7 +32,7 @@ namespace detail @note from https://stackoverflow.com/a/1001328/266378 */ -static bool little_endianess(int num = 1) noexcept +static inline bool little_endianess(int num = 1) noexcept { return *reinterpret_cast(&num) == 1; } diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 9c6e615b58..c4cf618c2a 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -194,9 +194,9 @@ class binary_writer } else { - if (j.m_value.number_float >= std::numeric_limits::lowest() and - j.m_value.number_float <= std::numeric_limits::max() and - static_cast(static_cast(j.m_value.number_float)) == j.m_value.number_float) + if (static_cast(j.m_value.number_float) >= static_cast(std::numeric_limits::lowest()) and + static_cast(j.m_value.number_float) <= static_cast(std::numeric_limits::max()) and + static_cast(static_cast(j.m_value.number_float)) == static_cast(j.m_value.number_float)) { oa->write_character(get_cbor_float_prefix(static_cast(j.m_value.number_float))); write_number(static_cast(j.m_value.number_float)); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c5ef3f7f08..4db36ef289 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5596,7 +5596,7 @@ namespace detail @note from https://stackoverflow.com/a/1001328/266378 */ -static bool little_endianess(int num = 1) noexcept +static inline bool little_endianess(int num = 1) noexcept { return *reinterpret_cast(&num) == 1; } @@ -12188,9 +12188,9 @@ class binary_writer } else { - if (j.m_value.number_float >= std::numeric_limits::lowest() and - j.m_value.number_float <= std::numeric_limits::max() and - static_cast(static_cast(j.m_value.number_float)) == j.m_value.number_float) + if (static_cast(j.m_value.number_float) >= static_cast(std::numeric_limits::lowest()) and + static_cast(j.m_value.number_float) <= static_cast(std::numeric_limits::max()) and + static_cast(static_cast(j.m_value.number_float)) == static_cast(j.m_value.number_float)) { oa->write_character(get_cbor_float_prefix(static_cast(j.m_value.number_float))); write_number(static_cast(j.m_value.number_float)); diff --git a/test/src/unit-cbor.cpp b/test/src/unit-cbor.cpp index 2800325876..2a53bb5295 100644 --- a/test/src/unit-cbor.cpp +++ b/test/src/unit-cbor.cpp @@ -939,7 +939,7 @@ TEST_CASE("CBOR") } SECTION("-3.40282e+38(lowest float)") { - double v = std::numeric_limits::lowest(); + double v = static_cast(std::numeric_limits::lowest()); json j = v; std::vector expected = { @@ -953,7 +953,7 @@ TEST_CASE("CBOR") } SECTION("1 + 3.40282e+38(more than max float)") { - double v = std::numeric_limits::max() + 0.1e+34; + double v = static_cast(std::numeric_limits::max()) + 0.1e+34; json j = v; std::vector expected = { @@ -968,7 +968,7 @@ TEST_CASE("CBOR") } SECTION("-1 - 3.40282e+38(less than lowest float)") { - double v = std::numeric_limits::lowest() - 1; + double v = static_cast(std::numeric_limits::lowest()) - 1.0; json j = v; std::vector expected = { @@ -1582,7 +1582,7 @@ TEST_CASE("CBOR") auto j = json::from_cbor(input); CHECK(j.is_binary()); auto k = json::binary_array({0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x99}); - CAPTURE(j.dump(0, ' ', false, json::error_handler_t::strict, true)); + CAPTURE(j.dump(0, ' ', false, json::error_handler_t::strict, true)) CHECK(j == k); } diff --git a/test/src/unit-noexcept.cpp b/test/src/unit-noexcept.cpp index 8fbe9f9165..6952c0ee81 100644 --- a/test/src/unit-noexcept.cpp +++ b/test/src/unit-noexcept.cpp @@ -46,7 +46,12 @@ void to_json(json&, pod) noexcept; void to_json(json&, pod_bis); void from_json(const json&, pod) noexcept; void from_json(const json&, pod_bis); -static json* j; +void to_json(json&, pod) noexcept {} +void to_json(json&, pod_bis) {} +void from_json(const json&, pod) noexcept {} +void from_json(const json&, pod_bis) {} + +static json* j = nullptr; static_assert(noexcept(json{}), ""); static_assert(noexcept(nlohmann::to_json(*j, 2)), ""); @@ -79,4 +84,14 @@ TEST_CASE("runtime checks") CHECK(std::is_nothrow_copy_constructible::value == std::is_nothrow_copy_constructible::value); CHECK(std::is_nothrow_copy_constructible::value == std::is_nothrow_copy_constructible::value); } + + SECTION("silence -Wunneeded-internal-declaration errors") + { + j = nullptr; + json j2; + to_json(j2, pod()); + to_json(j2, pod_bis()); + from_json(j2, pod()); + from_json(j2, pod_bis()); + } } diff --git a/test/src/unit-udt.cpp b/test/src/unit-udt.cpp index b1da8c3743..cd9ce69fb5 100644 --- a/test/src/unit-udt.cpp +++ b/test/src/unit-udt.cpp @@ -120,6 +120,8 @@ static void to_json(BasicJsonType& j, country c) case country::russia: j = u8"Российская Федерация"; return; + default: + break; } } @@ -803,7 +805,9 @@ class Evil public: Evil() = default; template - Evil(T) {} + Evil(T t) : m_i(sizeof(t)) {} + + int m_i = 0; }; void from_json(const json&, Evil&) {} @@ -816,6 +820,10 @@ TEST_CASE("Issue #924") CHECK_NOTHROW(j.get()); CHECK_NOTHROW(j.get>()); + + // silence Wunused-template warnings + Evil e(1); + CHECK(e.m_i >= 0); } TEST_CASE("Issue #1237") diff --git a/test/thirdparty/doctest/doctest.h b/test/thirdparty/doctest/doctest.h index 4f3a0e330c..2f0ff2131f 100755 --- a/test/thirdparty/doctest/doctest.h +++ b/test/thirdparty/doctest/doctest.h @@ -2913,7 +2913,7 @@ typedef timer_large_integer::type ticks_t; //unsigned int getElapsedMilliseconds() const { // return static_cast(getElapsedMicroseconds() / 1000); //} - double getElapsedSeconds() const { return (getCurrentTicks() - m_ticks) / 1000000.0; } + double getElapsedSeconds() const { return static_cast((getCurrentTicks() - m_ticks)) / 1000000.0; } private: ticks_t m_ticks = 0;