From 7feb2c20cfb37a52edcc92916e5fd48583064d35 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 30 Jul 2021 14:48:25 +0200 Subject: [PATCH 1/3] :rotating_light: fix useless-cast warnings --- cmake/ci.cmake | 2 +- .../nlohmann/detail/input/binary_reader.hpp | 5 +++-- include/nlohmann/detail/meta/type_traits.hpp | 14 +++++++++++++ single_include/nlohmann/json.hpp | 20 +++++++++++++++++-- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/cmake/ci.cmake b/cmake/ci.cmake index 8794ed078..7f2a56de6 100644 --- a/cmake/ci.cmake +++ b/cmake/ci.cmake @@ -356,7 +356,7 @@ set(GCC_CXXFLAGS "-std=c++11 \ -Wunused-result \ -Wunused-value \ -Wunused-variable \ - -Wno-useless-cast \ + -Wuseless-cast \ -Wvarargs \ -Wvariadic-macros \ -Wvector-operation-performance \ diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index f02a15e8f..8f55e1959 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include namespace nlohmann @@ -631,7 +632,7 @@ class binary_reader case 0x9B: // array (eight-byte uint64_t for n follow) { std::uint64_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast(len), tag_handler); + return get_number(input_format_t::cbor, len) && get_cbor_array(detail::conditional_static_cast(len), tag_handler); } case 0x9F: // array (indefinite length) @@ -685,7 +686,7 @@ class binary_reader case 0xBB: // map (eight-byte uint64_t for n follow) { std::uint64_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast(len), tag_handler); + return get_number(input_format_t::cbor, len) && get_cbor_object(detail::conditional_static_cast(len), tag_handler); } case 0xBF: // map (indefinite length) diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 163eb01be..4ee945cc9 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -435,5 +435,19 @@ struct is_constructible_tuple : std::false_type {}; template struct is_constructible_tuple> : conjunction...> {}; + +// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324) +template < typename T, typename U, std::enable_if_t < !std::is_same::value, int > = 0 > +T conditional_static_cast(U value) +{ + return static_cast(value); +} + +template::value, int> = 0> +T conditional_static_cast(U value) +{ + return value; +} + } // namespace detail } // namespace nlohmann diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 9185ea62f..07c1e1ab4 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3835,6 +3835,20 @@ struct is_constructible_tuple : std::false_type {}; template struct is_constructible_tuple> : conjunction...> {}; + +// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324) +template < typename T, typename U, std::enable_if_t < !std::is_same::value, int > = 0 > +T conditional_static_cast(U value) +{ + return static_cast(value); +} + +template::value, int> = 0> +T conditional_static_cast(U value) +{ + return value; +} + } // namespace detail } // namespace nlohmann @@ -8240,6 +8254,8 @@ struct is_sax_static_asserts } // namespace detail } // namespace nlohmann +// #include + // #include @@ -8853,7 +8869,7 @@ class binary_reader case 0x9B: // array (eight-byte uint64_t for n follow) { std::uint64_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast(len), tag_handler); + return get_number(input_format_t::cbor, len) && get_cbor_array(detail::conditional_static_cast(len), tag_handler); } case 0x9F: // array (indefinite length) @@ -8907,7 +8923,7 @@ class binary_reader case 0xBB: // map (eight-byte uint64_t for n follow) { std::uint64_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast(len), tag_handler); + return get_number(input_format_t::cbor, len) && get_cbor_object(detail::conditional_static_cast(len), tag_handler); } case 0xBF: // map (indefinite length) From 9a459e1bd5b2bbbb5f119085e7d56153cb24638c Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 30 Jul 2021 15:20:22 +0200 Subject: [PATCH 2/3] :rotating_light: fix useless-cast warnings --- include/nlohmann/detail/meta/type_traits.hpp | 4 ++-- single_include/nlohmann/json.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/meta/type_traits.hpp b/include/nlohmann/detail/meta/type_traits.hpp index 4ee945cc9..ee028b5d2 100644 --- a/include/nlohmann/detail/meta/type_traits.hpp +++ b/include/nlohmann/detail/meta/type_traits.hpp @@ -437,13 +437,13 @@ template struct is_constructible_tuple> : conjunction...> {}; // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324) -template < typename T, typename U, std::enable_if_t < !std::is_same::value, int > = 0 > +template < typename T, typename U, enable_if_t < !std::is_same::value, int > = 0 > T conditional_static_cast(U value) { return static_cast(value); } -template::value, int> = 0> +template::value, int> = 0> T conditional_static_cast(U value) { return value; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 07c1e1ab4..28ce55292 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3837,13 +3837,13 @@ template struct is_constructible_tuple> : conjunction...> {}; // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324) -template < typename T, typename U, std::enable_if_t < !std::is_same::value, int > = 0 > +template < typename T, typename U, enable_if_t < !std::is_same::value, int > = 0 > T conditional_static_cast(U value) { return static_cast(value); } -template::value, int> = 0> +template::value, int> = 0> T conditional_static_cast(U value) { return value; From f4716a0d4252f68b820ffe0e0390ea820a98f187 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 30 Jul 2021 21:12:55 +0200 Subject: [PATCH 3/3] :rotating_light: fix useless-cast warnings --- test/src/unit-class_parser.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index b0cdb0b35..3248ad9cf 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -1381,7 +1381,7 @@ TEST_CASE("parser class") case ('r'): case ('t'): { - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(s)).accept()); break; } @@ -1394,7 +1394,7 @@ TEST_CASE("parser class") // any other combination of backslash and character is invalid default: { - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(s)).accept() == false); break; } } @@ -1445,35 +1445,35 @@ TEST_CASE("parser class") std::string s = "\"\\u"; // create a string with the iterated character at each position - auto s1 = s + "000" + std::string(1, static_cast(c)) + "\""; - auto s2 = s + "00" + std::string(1, static_cast(c)) + "0\""; - auto s3 = s + "0" + std::string(1, static_cast(c)) + "00\""; - auto s4 = s + std::string(1, static_cast(c)) + "000\""; + const auto s1 = s + "000" + std::string(1, static_cast(c)) + "\""; + const auto s2 = s + "00" + std::string(1, static_cast(c)) + "0\""; + const auto s3 = s + "0" + std::string(1, static_cast(c)) + "00\""; + const auto s4 = s + std::string(1, static_cast(c)) + "000\""; if (valid(c)) { CAPTURE(s1) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s1))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(s1)).accept()); CAPTURE(s2) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s2))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(s2)).accept()); CAPTURE(s3) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s3))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(s3)).accept()); CAPTURE(s4) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s4))).accept()); + CHECK(json::parser(nlohmann::detail::input_adapter(s4)).accept()); } else { CAPTURE(s1) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s1))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(s1)).accept() == false); CAPTURE(s2) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s2))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(s2)).accept() == false); CAPTURE(s3) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s3))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(s3)).accept() == false); CAPTURE(s4) - CHECK(json::parser(nlohmann::detail::input_adapter(std::string(s4))).accept() == false); + CHECK(json::parser(nlohmann::detail::input_adapter(s4)).accept() == false); } } }