Skip to content

Commit

Permalink
🚨 fix warnings #2113
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed May 16, 2020
1 parent f40a9f8 commit 28e20bd
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -233,7 +236,7 @@ pedantic_gcc:
-Wno-system-headers \
-Wno-templates \
-Wno-undef \
-Wnoexcept \
-Wno-noexcept \
-Wnoexcept-type \
-Wnon-template-friend \
-Wnon-virtual-dtor \
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(&num) == 1;
}
Expand Down
6 changes: 3 additions & 3 deletions include/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ class binary_writer
}
else
{
if (j.m_value.number_float >= std::numeric_limits<float>::lowest() and
j.m_value.number_float <= std::numeric_limits<float>::max() and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float)
if (static_cast<double>(j.m_value.number_float) >= static_cast<double>(std::numeric_limits<float>::lowest()) and
static_cast<double>(j.m_value.number_float) <= static_cast<double>(std::numeric_limits<float>::max()) and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == static_cast<double>(j.m_value.number_float))
{
oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float)));
write_number(static_cast<float>(j.m_value.number_float));
Expand Down
8 changes: 4 additions & 4 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(&num) == 1;
}
Expand Down Expand Up @@ -12188,9 +12188,9 @@ class binary_writer
}
else
{
if (j.m_value.number_float >= std::numeric_limits<float>::lowest() and
j.m_value.number_float <= std::numeric_limits<float>::max() and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == j.m_value.number_float)
if (static_cast<double>(j.m_value.number_float) >= static_cast<double>(std::numeric_limits<float>::lowest()) and
static_cast<double>(j.m_value.number_float) <= static_cast<double>(std::numeric_limits<float>::max()) and
static_cast<double>(static_cast<float>(j.m_value.number_float)) == static_cast<double>(j.m_value.number_float))
{
oa->write_character(get_cbor_float_prefix(static_cast<float>(j.m_value.number_float)));
write_number(static_cast<float>(j.m_value.number_float));
Expand Down
8 changes: 4 additions & 4 deletions test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ TEST_CASE("CBOR")
}
SECTION("-3.40282e+38(lowest float)")
{
double v = std::numeric_limits<float>::lowest();
double v = static_cast<double>(std::numeric_limits<float>::lowest());
json j = v;
std::vector<uint8_t> expected =
{
Expand All @@ -953,7 +953,7 @@ TEST_CASE("CBOR")
}
SECTION("1 + 3.40282e+38(more than max float)")
{
double v = std::numeric_limits<float>::max() + 0.1e+34;
double v = static_cast<double>(std::numeric_limits<float>::max()) + 0.1e+34;
json j = v;
std::vector<uint8_t> expected =
{
Expand All @@ -968,7 +968,7 @@ TEST_CASE("CBOR")
}
SECTION("-1 - 3.40282e+38(less than lowest float)")
{
double v = std::numeric_limits<float>::lowest() - 1;
double v = static_cast<double>(std::numeric_limits<float>::lowest()) - 1.0;
json j = v;
std::vector<uint8_t> expected =
{
Expand Down Expand Up @@ -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);
}

Expand Down
17 changes: 16 additions & 1 deletion test/src/unit-noexcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)), "");
Expand Down Expand Up @@ -79,4 +84,14 @@ TEST_CASE("runtime checks")
CHECK(std::is_nothrow_copy_constructible<json::out_of_range>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
CHECK(std::is_nothrow_copy_constructible<json::other_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::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());
}
}
10 changes: 9 additions & 1 deletion test/src/unit-udt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ static void to_json(BasicJsonType& j, country c)
case country::russia:
j = u8"Российская Федерация";
return;
default:
break;
}
}

Expand Down Expand Up @@ -803,7 +805,9 @@ class Evil
public:
Evil() = default;
template <typename T>
Evil(T) {}
Evil(T t) : m_i(sizeof(t)) {}

int m_i = 0;
};

void from_json(const json&, Evil&) {}
Expand All @@ -816,6 +820,10 @@ TEST_CASE("Issue #924")

CHECK_NOTHROW(j.get<Evil>());
CHECK_NOTHROW(j.get<std::vector<Evil>>());

// silence Wunused-template warnings
Evil e(1);
CHECK(e.m_i >= 0);
}

TEST_CASE("Issue #1237")
Expand Down
2 changes: 1 addition & 1 deletion test/thirdparty/doctest/doctest.h
Original file line number Diff line number Diff line change
Expand Up @@ -2913,7 +2913,7 @@ typedef timer_large_integer::type ticks_t;
//unsigned int getElapsedMilliseconds() const {
// return static_cast<unsigned int>(getElapsedMicroseconds() / 1000);
//}
double getElapsedSeconds() const { return (getCurrentTicks() - m_ticks) / 1000000.0; }
double getElapsedSeconds() const { return static_cast<double>((getCurrentTicks() - m_ticks)) / 1000000.0; }

private:
ticks_t m_ticks = 0;
Expand Down

0 comments on commit 28e20bd

Please sign in to comment.