From 062702aed5fc5ba9f37f94782e8e341ba41df654 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 30 Jul 2022 09:58:09 +0200 Subject: [PATCH 1/3] Add regression tests for #3204 and #3333 --- tests/src/unit-regression2.cpp | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index 63df1a63b4..7c0bcb4da9 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -270,6 +270,56 @@ inline void from_json(const json& j, for_3312& obj) } #endif +///////////////////////////////////////////////////////////////////// +// for #3204 +///////////////////////////////////////////////////////////////////// + +struct for_3204_foo +{ + for_3204_foo() = default; + for_3204_foo(std::string /*unused*/) {} +}; + +struct for_3204_bar +{ + enum constructed_from_t + { + constructed_from_none = 0, + constructed_from_foo = 1, + constructed_from_json = 2 + }; + + for_3204_bar(std::function /*unused*/) // NOLINT(performance-unnecessary-value-param) + : constructed_from(constructed_from_foo) {} + for_3204_bar(std::function /*unused*/) // NOLINT(performance-unnecessary-value-param) + : constructed_from(constructed_from_json) {} + + constructed_from_t constructed_from = constructed_from_none; +}; + +///////////////////////////////////////////////////////////////////// +// for #3333 +///////////////////////////////////////////////////////////////////// + +struct for_3333 final +{ + for_3333(int x_ = 0, int y_ = 0) : x(x_), y(y_) {} + + template + for_3333(const T& /*unused*/) + { + CHECK(false); + } + + int x = 0; + int y = 0; +}; + +template <> +inline for_3333::for_3333(const json& json) + : for_3333(json.value("x", 0), json.value("y", 0)) +{} + TEST_CASE("regression tests 2") { SECTION("issue #1001 - Fix memory leak during parser callback") @@ -862,6 +912,28 @@ TEST_CASE("regression tests 2") CHECK(a2.type() == typeid(j)); } #endif + + SECTION("issue #3204 - ambiguous regression") + { + for_3204_bar bar_from_foo([](for_3204_foo) {}); + for_3204_bar bar_from_json([](json) {}); + + CHECK(bar_from_foo.constructed_from == for_3204_bar::constructed_from_foo); + CHECK(bar_from_json.constructed_from == for_3204_bar::constructed_from_json); + } + + SECTION("issue #3333 - Ambiguous conversion from nlohmann::basic_json<> to custom class") + { + const json j + { + {"x", 1}, + {"y", 2} + }; + for_3333 p = j; + + CHECK(p.x == 1); + CHECK(p.y == 2); + } } DOCTEST_CLANG_SUPPRESS_WARNING_POP From 644a00c994875479e72433a2f16d92f4e01ef522 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 30 Jul 2022 11:22:26 +0200 Subject: [PATCH 2/3] Fix typo in .clang-tidy --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index b981348f37..4d4238a355 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -43,7 +43,7 @@ Checks: '*, -readability-identifier-length, -readability-magic-numbers, -readability-redundant-access-specifiers, - -readability-simplify-boolean-expr + -readability-simplify-boolean-expr, -readability-uppercase-literal-suffix' CheckOptions: From 0117c9fac46014d6702070adaaa395be7fe4e6c6 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Sat, 30 Jul 2022 11:21:48 +0200 Subject: [PATCH 3/3] Fix warnings --- tests/src/unit-regression2.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/src/unit-regression2.cpp b/tests/src/unit-regression2.cpp index 7c0bcb4da9..8a1d9fdd82 100644 --- a/tests/src/unit-regression2.cpp +++ b/tests/src/unit-regression2.cpp @@ -277,7 +277,7 @@ inline void from_json(const json& j, for_3312& obj) struct for_3204_foo { for_3204_foo() = default; - for_3204_foo(std::string /*unused*/) {} + explicit for_3204_foo(std::string /*unused*/) {} // NOLINT(performance-unnecessary-value-param) }; struct for_3204_bar @@ -289,9 +289,9 @@ struct for_3204_bar constructed_from_json = 2 }; - for_3204_bar(std::function /*unused*/) // NOLINT(performance-unnecessary-value-param) + explicit for_3204_bar(std::function /*unused*/) noexcept // NOLINT(performance-unnecessary-value-param) : constructed_from(constructed_from_foo) {} - for_3204_bar(std::function /*unused*/) // NOLINT(performance-unnecessary-value-param) + explicit for_3204_bar(std::function /*unused*/) noexcept // NOLINT(performance-unnecessary-value-param) : constructed_from(constructed_from_json) {} constructed_from_t constructed_from = constructed_from_none; @@ -316,8 +316,8 @@ struct for_3333 final }; template <> -inline for_3333::for_3333(const json& json) - : for_3333(json.value("x", 0), json.value("y", 0)) +inline for_3333::for_3333(const json& j) + : for_3333(j.value("x", 0), j.value("y", 0)) {} TEST_CASE("regression tests 2") @@ -915,8 +915,8 @@ TEST_CASE("regression tests 2") SECTION("issue #3204 - ambiguous regression") { - for_3204_bar bar_from_foo([](for_3204_foo) {}); - for_3204_bar bar_from_json([](json) {}); + for_3204_bar bar_from_foo([](for_3204_foo) noexcept {}); // NOLINT(performance-unnecessary-value-param) + for_3204_bar bar_from_json([](json) noexcept {}); // NOLINT(performance-unnecessary-value-param) CHECK(bar_from_foo.constructed_from == for_3204_bar::constructed_from_foo); CHECK(bar_from_json.constructed_from == for_3204_bar::constructed_from_json);