Skip to content

Commit f4658de

Browse files
Fix 'declaration hides global declaration' warning (nlohmann#3751)
1 parent 3d1252b commit f4658de

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

tests/src/unit-noexcept.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ using nlohmann::json;
1818

1919
namespace
2020
{
21-
enum test
22-
{
23-
};
21+
enum test {};
2422

2523
struct pod {};
2624
struct pod_bis {};
@@ -34,25 +32,29 @@ void to_json(json& /*unused*/, pod_bis /*unused*/) {}
3432
void from_json(const json& /*unused*/, pod /*unused*/) noexcept {}
3533
void from_json(const json& /*unused*/, pod_bis /*unused*/) {}
3634

37-
json* j = nullptr;
38-
3935
static_assert(noexcept(json{}), "");
40-
static_assert(noexcept(nlohmann::to_json(*j, 2)), "");
41-
static_assert(noexcept(nlohmann::to_json(*j, 2.5)), "");
42-
static_assert(noexcept(nlohmann::to_json(*j, true)), "");
43-
static_assert(noexcept(nlohmann::to_json(*j, test{})), "");
44-
static_assert(noexcept(nlohmann::to_json(*j, pod{})), "");
45-
static_assert(!noexcept(nlohmann::to_json(*j, pod_bis{})), "");
36+
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), 2)), "");
37+
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), 2.5)), "");
38+
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), true)), "");
39+
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), test{})), "");
40+
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), pod{})), "");
41+
static_assert(!noexcept(nlohmann::to_json(std::declval<json&>(), pod_bis{})), "");
4642
static_assert(noexcept(json(2)), "");
4743
static_assert(noexcept(json(test{})), "");
4844
static_assert(noexcept(json(pod{})), "");
49-
static_assert(noexcept(j->get<pod>()), "");
50-
static_assert(!noexcept(j->get<pod_bis>()), "");
45+
static_assert(noexcept(std::declval<json>().get<pod>()), "");
46+
static_assert(!noexcept(std::declval<json>().get<pod_bis>()), "");
5147
static_assert(noexcept(json(pod{})), "");
5248
} // namespace
5349

54-
TEST_CASE("runtime checks")
50+
TEST_CASE("noexcept")
5551
{
52+
// silence -Wunneeded-internal-declaration errors
53+
static_cast<void>(static_cast<void(*)(json&, pod)>(&to_json));
54+
static_cast<void>(static_cast<void(*)(json&, pod_bis)>(&to_json));
55+
static_cast<void>(static_cast<void(*)(const json&, pod)>(&from_json));
56+
static_cast<void>(static_cast<void(*)(const json&, pod_bis)>(&from_json));
57+
5658
SECTION("nothrow-copy-constructible exceptions")
5759
{
5860
// for ERR60-CPP (https://github.com/nlohmann/json/issues/531):
@@ -67,16 +69,6 @@ TEST_CASE("runtime checks")
6769
CHECK(std::is_nothrow_copy_constructible<json::out_of_range>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
6870
CHECK(std::is_nothrow_copy_constructible<json::other_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
6971
}
70-
71-
SECTION("silence -Wunneeded-internal-declaration errors")
72-
{
73-
j = nullptr;
74-
json j2;
75-
to_json(j2, pod());
76-
to_json(j2, pod_bis());
77-
from_json(j2, pod());
78-
from_json(j2, pod_bis());
79-
}
8072
}
8173

8274
DOCTEST_GCC_SUPPRESS_WARNING_POP

0 commit comments

Comments
 (0)