Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useless-cast warnings #2902

Merged
merged 3 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/ci.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
5 changes: 3 additions & 2 deletions include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <nlohmann/detail/input/lexer.hpp>
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/meta/is_sax.hpp>
#include <nlohmann/detail/meta/type_traits.hpp>
#include <nlohmann/detail/value_t.hpp>

namespace nlohmann
Expand Down Expand Up @@ -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<std::size_t>(len), tag_handler);
return get_number(input_format_t::cbor, len) && get_cbor_array(detail::conditional_static_cast<std::size_t>(len), tag_handler);
}

case 0x9F: // array (indefinite length)
Expand Down Expand Up @@ -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<std::size_t>(len), tag_handler);
return get_number(input_format_t::cbor, len) && get_cbor_object(detail::conditional_static_cast<std::size_t>(len), tag_handler);
}

case 0xBF: // map (indefinite length)
Expand Down
14 changes: 14 additions & 0 deletions include/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,5 +435,19 @@ struct is_constructible_tuple : std::false_type {};

template<typename T1, typename... Args>
struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};

// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
T conditional_static_cast(U value)
{
return static_cast<T>(value);
}

template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
T conditional_static_cast(U value)
{
return value;
}

} // namespace detail
} // namespace nlohmann
20 changes: 18 additions & 2 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3835,6 +3835,20 @@ struct is_constructible_tuple : std::false_type {};

template<typename T1, typename... Args>
struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};

// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
T conditional_static_cast(U value)
{
return static_cast<T>(value);
}

template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
T conditional_static_cast(U value)
{
return value;
}

} // namespace detail
} // namespace nlohmann

Expand Down Expand Up @@ -8240,6 +8254,8 @@ struct is_sax_static_asserts
} // namespace detail
} // namespace nlohmann

// #include <nlohmann/detail/meta/type_traits.hpp>

// #include <nlohmann/detail/value_t.hpp>


Expand Down Expand Up @@ -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<std::size_t>(len), tag_handler);
return get_number(input_format_t::cbor, len) && get_cbor_array(detail::conditional_static_cast<std::size_t>(len), tag_handler);
}

case 0x9F: // array (indefinite length)
Expand Down Expand Up @@ -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<std::size_t>(len), tag_handler);
return get_number(input_format_t::cbor, len) && get_cbor_object(detail::conditional_static_cast<std::size_t>(len), tag_handler);
}

case 0xBF: // map (indefinite length)
Expand Down
28 changes: 14 additions & 14 deletions test/src/unit-class_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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<char>(c)) + "\"";
auto s2 = s + "00" + std::string(1, static_cast<char>(c)) + "0\"";
auto s3 = s + "0" + std::string(1, static_cast<char>(c)) + "00\"";
auto s4 = s + std::string(1, static_cast<char>(c)) + "000\"";
const auto s1 = s + "000" + std::string(1, static_cast<char>(c)) + "\"";
const auto s2 = s + "00" + std::string(1, static_cast<char>(c)) + "0\"";
const auto s3 = s + "0" + std::string(1, static_cast<char>(c)) + "00\"";
const auto s4 = s + std::string(1, static_cast<char>(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);
}
}
}
Expand Down