What changed, and why it matters
This is a small, clean code-quality change that adds a standard-style accessor method to a utility class. It does not fix a bug, close a security hole, or change any behavior that external users can reach. There is no security relevance.
No security action needed. Treat as normal code-quality/maintenance review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds Unexpected::error() const/non-const/lvalue/rvalue overloads to util/expected.h, mirroring std::unexpected’s API. It renames the public member ‘err’ to private ‘m_error’ and updates the single internal caller in Expected’s constructor. Tests are added to verify accessor and move semantics. No functional or security behavior changes.
Changed components
src/util/expected.hsrc/test/util_expected_tests.cppInspect captured patch +27 / −3
diff --git a/src/test/util_expected_tests.cpp b/src/test/util_expected_tests.cpp
index 67a1a5fb..f02ea3fd 100644
--- a/src/test/util_expected_tests.cpp
+++ b/src/test/util_expected_tests.cpp
@@ -7,6 +7,11 @@
#include <boost/test/unit_test.hpp>
+#include <memory>
+#include <string>
+#include <utility>
+
+
using namespace util;
BOOST_AUTO_TEST_SUITE(util_expected_tests)
@@ -65,4 +70,17 @@ BOOST_AUTO_TEST_CASE(expected_error)
BOOST_CHECK_EQUAL(read.error(), "fail1");
}
+BOOST_AUTO_TEST_CASE(unexpected_error_accessors)
+{
+ Unexpected u{std::make_unique<int>(-1)};
+ BOOST_CHECK_EQUAL(*u.error(), -1);
+
+ *u.error() -= 1;
+ const auto& read{u};
+ BOOST_CHECK_EQUAL(*read.error(), -2);
+
+ const auto moved{std::move(u).error()};
+ BOOST_CHECK_EQUAL(*moved, -2);
+}
+
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/util/expected.h b/src/util/expected.h
index 0e7256f8..71cd0f94 100644
--- a/src/util/expected.h
+++ b/src/util/expected.h
@@ -20,8 +20,14 @@ template <class E>
class Unexpected
{
public:
- constexpr explicit Unexpected(E e) : err(std::move(e)) {}
- E err;
+ constexpr explicit Unexpected(E e) : m_error(std::move(e)) {}
+
+ constexpr const E& error() const& noexcept LIFETIMEBOUND { return m_error; }
+ constexpr E& error() & noexcept LIFETIMEBOUND { return m_error; }
+ constexpr E&& error() && noexcept LIFETIMEBOUND { return std::move(m_error); }
+
+private:
+ E m_error;
};
/// The util::Expected class provides a standard way for low-level functions to
@@ -40,7 +46,7 @@ public:
constexpr Expected() : m_data{std::in_place_index_t<0>{}, ValueType{}} {}
constexpr Expected(ValueType v) : m_data{std::in_place_index_t<0>{}, std::move(v)} {}
template <class Err>
- constexpr Expected(Unexpected<Err> u) : m_data{std::in_place_index_t<1>{}, std::move(u.err)}
+ constexpr Expected(Unexpected<Err> u) : m_data{std::in_place_index_t<1>{}, std::move(u).error()}
{
}
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.