From 9a35702da80b78ef1c157434a68f1d17a94328ba Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 13 Oct 2021 20:34:17 +0200 Subject: [PATCH 1/8] :rewind: remove "fix" that caused #3077 --- include/nlohmann/json.hpp | 2 +- single_include/nlohmann/json.hpp | 2 +- test/src/unit-regression2.cpp | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b140577f2c..19c6011835 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3069,7 +3069,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), std::declval()))) { - ValueType ret{}; + ValueType ret; JSONSerializer::from_json(*this, ret); return ret; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 25c6983b04..e41ff9ec94 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20556,7 +20556,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), std::declval()))) { - ValueType ret{}; + ValueType ret; JSONSerializer::from_json(*this, ret); return ret; } diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index cf74297825..e84b8bd81e 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -189,6 +189,32 @@ template class my_allocator : public std::allocator {}; +///////////////////////////////////////////////////////////////////// +// for #3077 +///////////////////////////////////////////////////////////////////// + +class FooAlloc +{}; + +class Foo +{ + public: + explicit Foo(const FooAlloc& = FooAlloc()) : value(false) {} + + bool value; +}; + +class FooBar +{ + public: + Foo foo; +}; + +inline void from_json(const nlohmann::json& j, FooBar& fb) +{ + j.at("value").get_to(fb.foo.value); +} + TEST_CASE("regression tests 2") { SECTION("issue #1001 - Fix memory leak during parser callback") @@ -695,6 +721,14 @@ TEST_CASE("regression tests 2") json k = json::from_cbor(my_vector); CHECK(j == k); } + + SECTION("issue #3077 - explicit constructor with default does not compile") + { + json j; + j[0]["value"] = true; + std::vector foo; + j.get_to(foo); + } } DOCTEST_CLANG_SUPPRESS_WARNING_POP From 223bf3d143379d5cd7c141527aa8087c6affd4ca Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 13 Oct 2021 20:59:37 +0200 Subject: [PATCH 2/8] :mute: silence Clang-Tidy --- include/nlohmann/json.hpp | 1 + single_include/nlohmann/json.hpp | 1 + 2 files changed, 2 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 19c6011835..40569bb859 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3069,6 +3069,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), std::declval()))) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init): see https://github.com/nlohmann/json/issues/3077 ValueType ret; JSONSerializer::from_json(*this, ret); return ret; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e41ff9ec94..09db75fdac 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20556,6 +20556,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), std::declval()))) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init): see https://github.com/nlohmann/json/issues/3077 ValueType ret; JSONSerializer::from_json(*this, ret); return ret; From 39928f5f8c73b8b75c8b74a1b639a711c618d0c3 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 13 Oct 2021 21:23:30 +0200 Subject: [PATCH 3/8] :rotating_light: fix warnings unit-regression2.cpp:202:34: error: all parameters should be named in a function [hicpp-named-parameter,readability-named-parameter,-warnings-as-errors] explicit Foo(const FooAlloc& = FooAlloc()) : value(false) {} ^ /*unused*/ unit-regression2.cpp:204:10: error: use default member initializer for 'value' [modernize-use-default-member-init,-warnings-as-errors] bool value; ^ {false} --- test/src/unit-regression2.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index e84b8bd81e..2245241893 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -199,9 +199,9 @@ class FooAlloc class Foo { public: - explicit Foo(const FooAlloc& = FooAlloc()) : value(false) {} + explicit Foo(const FooAlloc& /* unused */ = FooAlloc()) : value(false) {} - bool value; + bool value = false; }; class FooBar From 346c00e17c1583756205714730494d23081935c9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 13 Oct 2021 21:24:07 +0200 Subject: [PATCH 4/8] :rotating_light: fix warning Initialization of "value" is redundant. --- test/src/unit-regression2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 2245241893..4243cc609d 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -199,7 +199,7 @@ class FooAlloc class Foo { public: - explicit Foo(const FooAlloc& /* unused */ = FooAlloc()) : value(false) {} + explicit Foo(const FooAlloc& /* unused */ = FooAlloc()) {} bool value = false; }; From d6740b4abd27394b9dcc128132ddaa730d6a95a8 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 13 Oct 2021 21:28:09 +0200 Subject: [PATCH 5/8] :rotating_light: fix warning unit-regression2.cpp:207:7: error: 'FooBar::foo' should be initialized in the member initialization list [-Werror=effc++] 207 | class FooBar | ^~~~~~ --- test/src/unit-regression2.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 4243cc609d..b46cc5c762 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -727,7 +727,10 @@ TEST_CASE("regression tests 2") json j; j[0]["value"] = true; std::vector foo; + DOCTEST_GCC_SUPPRESS_WARNING_PUSH + DOCTEST_GCC_SUPPRESS_WARNING(-Weffc++) j.get_to(foo); + DOCTEST_GCC_SUPPRESS_WARNING_POP } } From d698b6bf262610732d7a9c956d1c29620e0f4e15 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 13 Oct 2021 22:22:05 +0200 Subject: [PATCH 6/8] :mute: silence -Weffc++ warning error: 'FooBar::foo' should be initialized in the member initialization list [-Werror=effc++] --- include/nlohmann/json.hpp | 8 ++++++++ single_include/nlohmann/json.hpp | 8 ++++++++ test/src/unit-regression2.cpp | 3 --- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 40569bb859..269ee680a5 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3069,8 +3069,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), std::declval()))) { +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Weffc++" +#endif // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init): see https://github.com/nlohmann/json/issues/3077 ValueType ret; +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + JSONSerializer::from_json(*this, ret); return ret; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 09db75fdac..8ca12791cb 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20556,8 +20556,16 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), std::declval()))) { +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Weffc++" +#endif // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init): see https://github.com/nlohmann/json/issues/3077 ValueType ret; +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + JSONSerializer::from_json(*this, ret); return ret; } diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index b46cc5c762..4243cc609d 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -727,10 +727,7 @@ TEST_CASE("regression tests 2") json j; j[0]["value"] = true; std::vector foo; - DOCTEST_GCC_SUPPRESS_WARNING_PUSH - DOCTEST_GCC_SUPPRESS_WARNING(-Weffc++) j.get_to(foo); - DOCTEST_GCC_SUPPRESS_WARNING_POP } } From 28ba5e49fe0b10e6c56520b56d2c59ea42442684 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 14 Oct 2021 08:00:39 +0200 Subject: [PATCH 7/8] :recycle: default initialization --- include/nlohmann/json.hpp | 11 +---------- single_include/nlohmann/json.hpp | 11 +---------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 269ee680a5..6058d8fe10 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -3069,16 +3069,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), std::declval()))) { -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Weffc++" -#endif - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init): see https://github.com/nlohmann/json/issues/3077 - ValueType ret; -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif - + auto ret = ValueType(); JSONSerializer::from_json(*this, ret); return ret; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8ca12791cb..5cfd0086ff 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20556,16 +20556,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( JSONSerializer::from_json(std::declval(), std::declval()))) { -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Weffc++" -#endif - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init): see https://github.com/nlohmann/json/issues/3077 - ValueType ret; -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif - + auto ret = ValueType(); JSONSerializer::from_json(*this, ret); return ret; } From b05fcf1b6df7ce1b3538a494cd5d533b70d23fb7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 14 Oct 2021 08:41:10 +0200 Subject: [PATCH 8/8] :recycle: default initialization --- test/src/unit-regression2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 4243cc609d..a605c9eeb6 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -207,7 +207,7 @@ class Foo class FooBar { public: - Foo foo; + Foo foo{}; }; inline void from_json(const nlohmann::json& j, FooBar& fb)