What changed, and why it matters
This commit adds a standard swap() method to a small utility helper class (Expected) used to represent either a successful value or an error. It also adds a unit test. There is no security relevance in the change itself.
No action required. This is a routine utility enhancement with no security implications.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds Expected::swap(), implemented by delegating to std::variant::swap. The implementation is noexcept and constexpr. A unit test exercises swapping an error state with a value state. No existing behavior is modified, no bug is fixed, and no unsafe pattern is introduced.
Changed components
src/util/expected.hsrc/test/util_expected_tests.cppInspect captured patch +11 / −0
diff --git a/src/test/util_expected_tests.cpp b/src/test/util_expected_tests.cpp
index 356735b2..5979250e 100644
--- a/src/test/util_expected_tests.cpp
+++ b/src/test/util_expected_tests.cpp
@@ -122,4 +122,13 @@ BOOST_AUTO_TEST_CASE(unexpected_error_accessors)
BOOST_CHECK_EQUAL(*moved, -2);
}
+BOOST_AUTO_TEST_CASE(expected_swap)
+{
+ Expected<const char*, std::unique_ptr<int>> a{Unexpected{std::make_unique<int>(-1)}};
+ Expected<const char*, std::unique_ptr<int>> b{"good"};
+ a.swap(b);
+ BOOST_CHECK_EQUAL(a.value(), "good");
+ BOOST_CHECK_EQUAL(*b.error(), -1);
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/util/expected.h b/src/util/expected.h
index b01d866a..66fb98e0 100644
--- a/src/util/expected.h
+++ b/src/util/expected.h
@@ -88,6 +88,8 @@ public:
constexpr E& error() & noexcept LIFETIMEBOUND { return *Assert(std::get_if<1>(&m_data)); }
constexpr E&& error() && noexcept LIFETIMEBOUND { return std::move(error()); }
+ constexpr void swap(Expected& other) noexcept { m_data.swap(other.m_data); }
+
constexpr T& operator*() & noexcept LIFETIMEBOUND { return value(); }
constexpr const T& operator*() const& noexcept LIFETIMEBOUND { return value(); }
constexpr T&& operator*() && noexcept LIFETIMEBOUND { return std::move(value()); }
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.