[clang] [FlowSensitive][Expected] Model constructors, assignments and `emplace` in `UncheckedExpectedAccessModel` [PR 3/N] (PR #246771)
Valentyn Yukhymenko via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 11:31:09 PST 2026
https://github.com/BaLiKfromUA updated https://github.com/llvm/llvm-project/pull/246771
>From fe1bcd048ec5631d25c7f69dcb83c92b81c0b194 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <valentin.yukhymenko at gmail.com>
Date: Fri, 27 Nov 2026 17:24:36 +0000
Subject: [PATCH 1/7] basic tests for emplace
---
.../UncheckedExpectedAccessModelTest.cpp | 98 +++++++++++++++++++
1 file changed, 98 insertions(+)
diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
index 550209c73e9206..9d6f269cb9ab99 100644
--- a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
@@ -951,4 +951,102 @@ TEST_P(UncheckedExpectedAccessTest, EqualToUnexpectedUncheckedVoidValue) {
)");
}
+TEST_P(UncheckedExpectedAccessTest, Emplace) {
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<int, int> e) {
+ e.emplace(1);
+ e.value();
+ *e;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ struct Foo {
+ void foo() const {}
+ };
+
+ void target($ns::expected<Foo, int> e) {
+ e.emplace();
+ e->foo();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ if (!e)
+ e.emplace(0);
+ return *e;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> *p) {
+ p->emplace(1);
+ return **p;
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, EmplaceVoid) {
+ // `tl::expected<void, E>` has no `value()`.
+ if (GetParam().NamespaceName != "std")
+ GTEST_SKIP();
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<void, int> e) {
+ e.emplace();
+ e.value();
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, EmplaceUnchecked) {
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<int, int> e) {
+ e.emplace(1);
+ e.error(); // [[unsafe:error]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+ a.emplace(1);
+ return *b; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e, bool cond) {
+ if (cond)
+ e.emplace(1);
+ return *e; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<void, int> e) {
+ e.emplace();
+ e.error(); // [[unsafe:error]]
+ }
+ )");
+}
+
} // namespace clang::dataflow::test
>From 9ad7c8ff1507dff8f567f21e36011ec553ab90ab Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <valentin.yukhymenko at gmail.com>
Date: Fri, 27 Nov 2026 17:38:45 +0000
Subject: [PATCH 2/7] support `emplace` for basic scenarios
---
.../Models/UncheckedExpectedAccessModel.cpp | 24 ++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
index f0d5f3764f028a..4f2fae05f96ad6 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
@@ -170,6 +170,17 @@ BoolValue *getHasValue(Environment &Env, RecordStorageLocation *ExpectedLoc) {
return HasValueVal;
}
+/// Sets `HasValueVal` as the symbolic value that represents the "has_value"
+/// property of the expected at `ExpectedLoc`. Does nothing if `ExpectedLoc` is
+/// null or is not the location of a supported expected object (e.g. of a
+/// derived class object).
+void setHasValue(RecordStorageLocation *ExpectedLoc, BoolValue &HasValueVal,
+ Environment &Env) {
+ if (ExpectedLoc == nullptr || !isExpectedType(ExpectedLoc->getType()))
+ return;
+ Env.setValue(locForHasValue(*ExpectedLoc), HasValueVal);
+}
+
void transferHasValueCall(const CXXMemberCallExpr *E,
const MatchFinder::MatchResult &,
LatticeTransferState &State) {
@@ -178,6 +189,14 @@ void transferHasValueCall(const CXXMemberCallExpr *E,
State.Env.setValue(*E, *HasValueVal);
}
+void transferEmplaceCall(const CXXMemberCallExpr *E,
+ const MatchFinder::MatchResult &,
+ LatticeTransferState &State) {
+ // Every `emplace` overload leaves the expected holding a value.
+ setHasValue(getImplicitObjectLocation(*E, State.Env),
+ State.Env.getBoolLiteralValue(true), State.Env);
+}
+
void transferExpectedEqualityCall(const CXXOperatorCallExpr *E,
const MatchFinder::MatchResult &,
LatticeTransferState &State) {
@@ -233,13 +252,16 @@ void transferExpectedUnexpectedEqualityCall(const CXXOperatorCallExpr *E,
A.makeNot(HasValueVal->formula())));
}
-// FIXME: Model the constructors, assignments, `emplace` and `swap`.
+// FIXME: Model the constructors, assignments and `swap`.
auto buildTransferMatchSwitch() {
return CFGMatchSwitchBuilder<LatticeTransferState>()
// expected::has_value, expected::operator bool
.CaseOfCFGStmt<CXXMemberCallExpr>(
expectedMemberCall(hasAnyName("has_value", "operator bool")),
transferHasValueCall)
+ // expected::emplace
+ .CaseOfCFGStmt<CXXMemberCallExpr>(expectedMemberCall(hasName("emplace")),
+ transferEmplaceCall)
// operator==, operator!= between two expecteds
.CaseOfCFGStmt<CXXOperatorCallExpr>(expectedEqualityCall(),
transferExpectedEqualityCall)
>From 52b8142b8898f41d4e805aabcd68ab4ca68dfa65 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <valentin.yukhymenko at gmail.com>
Date: Fri, 27 Nov 2026 17:38:45 +0000
Subject: [PATCH 3/7] Tests for constructors
---
.../Analysis/FlowSensitive/MockHeaders.cpp | 49 +++-
.../UncheckedExpectedAccessModelTest.cpp | 273 ++++++++++++++++++
2 files changed, 317 insertions(+), 5 deletions(-)
diff --git a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
index 9e157efa964f91..91f2ab7bb42c24 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -824,6 +824,14 @@ static constexpr char StdExpectedHeader[] = R"(
#ifndef STD_EXPECTED_H
#define STD_EXPECTED_H
+// For `std::in_place_t` and `std::in_place`.
+#include "std_type_traits.h"
+
+// The mock uses C++20 features (`requires` clauses and rewritten comparison
+// candidates), but it is also included by the `tl::expected` tests in C++17
+// mode.
+#if __cplusplus >= 202002L
+
namespace std {
template <class E> class unexpected {
@@ -853,6 +861,22 @@ template <class> constexpr bool __is_expected = false;
template <class T, class E>
constexpr bool __is_expected<expected<T, E>> = true;
+template <class> constexpr bool __is_unexpected = false;
+template <class E> constexpr bool __is_unexpected<unexpected<E>> = true;
+
+// `__remove_cvref` is a clang builtin, so the helper has a different name.
+// clang-format off
+template <class T> struct __remove_cvref_impl { using type = T; };
+template <class T> struct __remove_cvref_impl<const T> { using type = T; };
+template <class T> struct __remove_cvref_impl<T &> { using type = T; };
+template <class T> struct __remove_cvref_impl<const T &> { using type = T; };
+template <class T> struct __remove_cvref_impl<T &&> { using type = T; };
+template <class T> struct __remove_cvref_impl<const T &&> { using type = T; };
+// clang-format on
+
+template <class T>
+using __remove_cvref_t = typename __remove_cvref_impl<T>::type;
+
template <class T, class E> class expected {
public:
using value_type = T;
@@ -863,17 +887,29 @@ template <class T, class E> class expected {
expected(const expected &);
expected(expected &&) noexcept;
- template <class U = T> expected(U &&v);
+ template <class U, class G> expected(const expected<U, G> &x);
+ template <class U, class G> expected(expected<U, G> &&x);
+
+ template <class U = T>
+ requires(!__is_expected<__remove_cvref_t<U>> &&
+ !__is_same(__remove_cvref_t<U>, in_place_t) &&
+ !__is_same(__remove_cvref_t<U>, unexpect_t) &&
+ !__is_unexpected<__remove_cvref_t<U>>)
+ expected(U &&v);
template <class G> expected(const unexpected<G> &e);
template <class G> expected(unexpected<G> &&e);
+ template <class... Args> explicit expected(in_place_t, Args &&...args);
+
template <class... Args>
explicit expected(unexpect_t, Args &&...args);
expected &operator=(const expected &);
expected &operator=(expected &&) noexcept;
+ template <class G> expected &operator=(unexpected<G> &&e);
+
const T *operator->() const noexcept;
T *operator->() noexcept;
@@ -907,7 +943,6 @@ template <class T, class E> class expected {
// The comparisons rely on C++20 rewritten candidates for `!=` and for
// reversed operands.
-#if __cplusplus >= 202002L
template <class T2, class E2>
friend bool operator==(const expected &x, const expected<T2, E2> &y);
@@ -917,7 +952,6 @@ template <class T, class E> class expected {
template <class E2>
friend bool operator==(const expected &x, const unexpected<E2> &e);
-#endif
};
template <class E> class expected<void, E> {
@@ -930,9 +964,14 @@ template <class E> class expected<void, E> {
expected(const expected &);
expected(expected &&) noexcept;
+ template <class U, class G> expected(const expected<U, G> &x);
+ template <class U, class G> expected(expected<U, G> &&x);
+
template <class G> expected(const unexpected<G> &e);
template <class G> expected(unexpected<G> &&e);
+ explicit expected(in_place_t) noexcept;
+
template <class... Args>
explicit expected(unexpect_t, Args &&...args);
@@ -959,17 +998,17 @@ template <class E> class expected<void, E> {
void swap(expected &rhs) noexcept;
-#if __cplusplus >= 202002L
template <class T2, class E2>
friend bool operator==(const expected &x, const expected<T2, E2> &y);
template <class E2>
friend bool operator==(const expected &x, const unexpected<E2> &e);
-#endif
};
} // namespace std
+#endif // __cplusplus >= 202002L
+
#endif // STD_EXPECTED_H
)";
diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
index 9d6f269cb9ab99..216f7b995aa618 100644
--- a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
@@ -1049,4 +1049,277 @@ TEST_P(UncheckedExpectedAccessTest, EmplaceUnchecked) {
)");
}
+TEST_P(UncheckedExpectedAccessTest, Construct) {
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e;
+ return *e;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e{};
+ return e.value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e(5);
+ return *e;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e = 5;
+ return *e;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ struct Foo {
+ void foo() const {}
+ };
+
+ void target() {
+ $ns::expected<Foo, int> e($ns::in_place);
+ e->foo();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ return $ns::expected<int, int>(5).value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e($ns::unexpected(1));
+ return e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ const $ns::unexpected<int> u(1);
+ $ns::expected<int, int> e(u);
+ return e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e = $ns::unexpected(1);
+ return e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e($ns::unexpect, 1);
+ return e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target() {
+ $ns::expected<void, int> e($ns::unexpect, 1);
+ e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<long, int> b) {
+ if (!b)
+ return 0;
+ $ns::expected<int, int> a(b);
+ return *a;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> b) {
+ if (!b)
+ return 0;
+ auto a = b;
+ return *a;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> b) {
+ if (!b)
+ return 0;
+ auto a = std::move(b);
+ return *a;
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, ConstructVoid) {
+ // `tl::expected<void, E>` has no `value()`.
+ if (GetParam().NamespaceName != "std")
+ GTEST_SKIP();
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target() {
+ $ns::expected<void, int> e;
+ e.value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target() {
+ $ns::expected<void, int> e($ns::in_place);
+ e.value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target() {
+ $ns::expected<void, int> e($ns::unexpect, 1);
+ e.value(); // [[unsafe]]
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, ConstructFromMakeUnexpected) {
+ // `make_unexpected` is only provided by `tl`.
+ if (GetParam().NamespaceName != "tl")
+ GTEST_SKIP();
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e = $ns::make_unexpected(1);
+ return e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e($ns::make_unexpected(1));
+ return *e; // [[unsafe]]
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, ConstructUnchecked) {
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e;
+ return e.error(); // [[unsafe:error]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target() {
+ $ns::expected<void, int> e;
+ e.error(); // [[unsafe:error]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e(5);
+ return e.error(); // [[unsafe:error]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e($ns::unexpected(1));
+ return *e; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e($ns::unexpect, 1);
+ return *e; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<long, int> b) {
+ $ns::expected<int, int> a(b);
+ return *a; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> b) {
+ auto a = b;
+ return *a; // [[unsafe]]
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, ConstructThenAssignUnexpected) {
+ // FIXME: should warn once converting assignments are modeled.
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target() {
+ $ns::expected<int, int> e;
+ e = $ns::unexpected(1);
+ return *e;
+ }
+ )");
+}
+
} // namespace clang::dataflow::test
>From 9b76c34b912d75d900ef977e140629743a5d3b18 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <valentin.yukhymenko at gmail.com>
Date: Fri, 27 Nov 2026 17:38:53 +0000
Subject: [PATCH 4/7] Test for expected inside expected
---
.../Analysis/FlowSensitive/MockHeaders.cpp | 44 +++++++++--
.../UncheckedExpectedAccessModelTest.cpp | 76 +++++++++++++++++++
2 files changed, 112 insertions(+), 8 deletions(-)
diff --git a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
index 91f2ab7bb42c24..e7ecbed390d537 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -877,6 +877,15 @@ template <class T> struct __remove_cvref_impl<const T &&> { using type = T; };
template <class T>
using __remove_cvref_t = typename __remove_cvref_impl<T>::type;
+// Whether `T` can be constructed from `expected<U, G>`, in which case the
+// converting constructors from `expected<U, G>` are disabled.
+template <class T, class U, class G>
+constexpr bool __cons_from_expected =
+ __is_constructible(T, expected<U, G> &) ||
+ __is_constructible(T, expected<U, G>) ||
+ __is_constructible(T, const expected<U, G> &) ||
+ __is_constructible(T, const expected<U, G>);
+
template <class T, class E> class expected {
public:
using value_type = T;
@@ -887,14 +896,21 @@ template <class T, class E> class expected {
expected(const expected &);
expected(expected &&) noexcept;
- template <class U, class G> expected(const expected<U, G> &x);
- template <class U, class G> expected(expected<U, G> &&x);
+ template <class U, class G>
+ requires(__is_constructible(T, const U &) &&
+ __is_constructible(E, const G &) &&
+ !__cons_from_expected<T, U, G>)
+ expected(const expected<U, G> &x);
+ template <class U, class G>
+ requires(__is_constructible(T, U) && __is_constructible(E, G) &&
+ !__cons_from_expected<T, U, G>)
+ expected(expected<U, G> &&x);
template <class U = T>
- requires(!__is_expected<__remove_cvref_t<U>> &&
+ requires(!__is_same(__remove_cvref_t<U>, expected) &&
!__is_same(__remove_cvref_t<U>, in_place_t) &&
!__is_same(__remove_cvref_t<U>, unexpect_t) &&
- !__is_unexpected<__remove_cvref_t<U>>)
+ !__is_unexpected<__remove_cvref_t<U>> && __is_constructible(T, U))
expected(U &&v);
template <class G> expected(const unexpected<G> &e);
@@ -1060,6 +1076,15 @@ namespace detail {
template <class> constexpr bool is_unexpected = false;
template <class E> constexpr bool is_unexpected<unexpected<E>> = true;
+// Whether `T` can be constructed from `expected<U, G>`, in which case the
+// converting constructors from `expected<U, G>` are disabled.
+template <class T, class U, class G>
+constexpr bool is_constructible_from_expected =
+ __is_constructible(T, expected<U, G> &) ||
+ __is_constructible(T, expected<U, G> &&) ||
+ __is_constructible(T, const expected<U, G> &) ||
+ __is_constructible(T, const expected<U, G> &&);
+
} // namespace detail
template <class T, class E> class expected {
@@ -1082,12 +1107,15 @@ template <class T, class E> class expected {
template <class... Args> explicit expected(unexpect_t, Args &&...args);
template <class U, class G,
- std::enable_if_t<__is_constructible(T, const U &) &&
- __is_constructible(E, const G &)> * = nullptr>
+ std::enable_if_t<
+ __is_constructible(T, const U &) &&
+ __is_constructible(E, const G &) &&
+ !detail::is_constructible_from_expected<T, U, G>> * = nullptr>
expected(const expected<U, G> &rhs);
template <class U, class G,
- std::enable_if_t<__is_constructible(T, U &&) &&
- __is_constructible(E, G &&)> * = nullptr>
+ std::enable_if_t<
+ __is_constructible(T, U &&) && __is_constructible(E, G &&) &&
+ !detail::is_constructible_from_expected<T, U, G>> * = nullptr>
expected(expected<U, G> &&rhs);
template <
diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
index 216f7b995aa618..8e3121f672cebc 100644
--- a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
@@ -1309,6 +1309,82 @@ TEST_P(UncheckedExpectedAccessTest, ConstructUnchecked) {
)");
}
+TEST_P(UncheckedExpectedAccessTest, ConstructNested) {
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target() {
+ $ns::expected<int, int> inner($ns::unexpect, 1);
+ $ns::expected<$ns::expected<int, int>, int> outer(inner);
+ outer.value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target() {
+ $ns::expected<$ns::expected<int, int>, int> outer(
+ $ns::expected<int, int>($ns::unexpect, 1));
+ outer.value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<int, int> inner) {
+ $ns::expected<$ns::expected<int, int>, int> outer(inner);
+ outer.value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<$ns::expected<long, int>, int> b) {
+ if (!b)
+ return;
+ $ns::expected<$ns::expected<int, int>, int> a(b);
+ a.value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ struct FromExpected {
+ FromExpected($ns::expected<int, int>);
+ };
+
+ void target($ns::expected<int, int> b) {
+ $ns::expected<FromExpected, int> a(b);
+ a.value();
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, ConstructNestedUnchecked) {
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target() {
+ $ns::expected<int, int> inner($ns::unexpect, 1);
+ $ns::expected<$ns::expected<int, int>, int> outer(inner);
+ outer.error(); // [[unsafe:error]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<$ns::expected<long, int>, int> b) {
+ $ns::expected<$ns::expected<int, int>, int> a(b);
+ a.value(); // [[unsafe]]
+ }
+ )");
+}
+
TEST_P(UncheckedExpectedAccessTest, ConstructThenAssignUnexpected) {
// FIXME: should warn once converting assignments are modeled.
ExpectDiagnosticsFor(R"(
>From 5980d5a6e9d498ef5f35058ec8421ec0c2b3c5a1 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <valentin.yukhymenko at gmail.com>
Date: Fri, 27 Nov 2026 17:38:54 +0000
Subject: [PATCH 5/7] model constructors of std::expected
---
.../Models/UncheckedExpectedAccessModel.cpp | 83 ++++++++++++++++++-
1 file changed, 82 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
index 4f2fae05f96ad6..dd4ed03310d823 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
@@ -120,6 +120,53 @@ auto expectedUnexpectedEqualityCall() {
return expectedComparisonCall(hasUnexpectedType());
}
+// Copy and move constructors are modeled by the framework, which copies the
+// "has_value" property of the source.
+auto expectedConstructor() {
+ return cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
+ ofClass(expectedClass()),
+ unless(anyOf(isCopyConstructor(), isMoveConstructor())))));
+}
+
+// `expected(unexpect_t, ...)`, `expected(const unexpected<G> &)` and
+// `expected(unexpected<G> &&)`.
+auto expectedErrorConstructor() {
+ return cxxConstructExpr(
+ expectedConstructor(),
+ hasArgument(0, anyOf(hasType(namedDecl(hasAnyName("std::unexpect_t",
+ "tl::unexpect_t"))),
+ hasUnexpectedType())));
+}
+
+// Matches `template <class U, class G> expected(const expected<U, G> &)` and
+// `template <class U, class G> expected(expected<U, G> &&)`. The argument type
+// alone is not enough to recognize them: `expected(U &&)` also accepts an
+// `expected` when `T` is constructible from it, e.g. for nested `expected`s.
+AST_MATCHER(CXXConstructorDecl, expectedConvertingConstructorDecl) {
+ const FunctionTemplateDecl *Template = Node.getPrimaryTemplate();
+ if (Template == nullptr)
+ return false;
+ const FunctionDecl *Pattern = Template->getTemplatedDecl();
+ if (Pattern->getNumParams() != 1)
+ return false;
+ const auto *ParamType = Pattern->getParamDecl(0)
+ ->getType()
+ .getNonReferenceType()
+ ->getAs<TemplateSpecializationType>();
+ if (ParamType == nullptr)
+ return false;
+ const auto *ParamTemplate = dyn_cast_or_null<ClassTemplateDecl>(
+ ParamType->getTemplateName().getAsTemplateDecl());
+ return ParamTemplate != nullptr &&
+ isExpectedClass(*ParamTemplate->getTemplatedDecl());
+}
+
+auto expectedConvertingConstructor() {
+ return cxxConstructExpr(
+ expectedConstructor(),
+ hasDeclaration(cxxConstructorDecl(expectedConvertingConstructorDecl())));
+}
+
/// Ensures that `E` is mapped to a `BoolValue` and returns its formula.
const Formula &forceBoolValue(Environment &Env, const Expr &E) {
auto *Value = Env.get<BoolValue>(E);
@@ -252,7 +299,33 @@ void transferExpectedUnexpectedEqualityCall(const CXXOperatorCallExpr *E,
A.makeNot(HasValueVal->formula())));
}
-// FIXME: Model the constructors, assignments and `swap`.
+void constructExpected(const CXXConstructExpr &E, BoolValue &HasValueVal,
+ Environment &Env) {
+ setHasValue(&Env.getResultObjectLocation(E), HasValueVal, Env);
+}
+
+void transferValueConstructor(const CXXConstructExpr *E,
+ const MatchFinder::MatchResult &,
+ LatticeTransferState &State) {
+ constructExpected(*E, State.Env.getBoolLiteralValue(true), State.Env);
+}
+
+void transferErrorConstructor(const CXXConstructExpr *E,
+ const MatchFinder::MatchResult &,
+ LatticeTransferState &State) {
+ constructExpected(*E, State.Env.getBoolLiteralValue(false), State.Env);
+}
+
+void transferConvertingConstructor(const CXXConstructExpr *E,
+ const MatchFinder::MatchResult &,
+ LatticeTransferState &State) {
+ // The new expected holds a value if and only if the source does.
+ if (BoolValue *HasValueVal = getHasValue(
+ State.Env, State.Env.get<RecordStorageLocation>(*E->getArg(0))))
+ constructExpected(*E, *HasValueVal, State.Env);
+}
+
+// FIXME: Model the assignments and `swap`.
auto buildTransferMatchSwitch() {
return CFGMatchSwitchBuilder<LatticeTransferState>()
// expected::has_value, expected::operator bool
@@ -272,6 +345,14 @@ auto buildTransferMatchSwitch() {
.CaseOfCFGStmt<CXXOperatorCallExpr>(
expectedUnexpectedEqualityCall(),
transferExpectedUnexpectedEqualityCall)
+ // Constructors. The order matters: the last case matches every
+ // constructor that is not matched by the ones before it.
+ .CaseOfCFGStmt<CXXConstructExpr>(expectedErrorConstructor(),
+ transferErrorConstructor)
+ .CaseOfCFGStmt<CXXConstructExpr>(expectedConvertingConstructor(),
+ transferConvertingConstructor)
+ .CaseOfCFGStmt<CXXConstructExpr>(expectedConstructor(),
+ transferValueConstructor)
.Build();
}
>From 775abcf98e85ffdfe8599e7e9f427239ac6ef024 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <valentin.yukhymenko at gmail.com>
Date: Fri, 27 Nov 2026 17:42:58 +0000
Subject: [PATCH 6/7] Add tests for assignments
---
.../Analysis/FlowSensitive/MockHeaders.cpp | 10 +
.../UncheckedExpectedAccessModelTest.cpp | 216 +++++++++++++++++-
2 files changed, 225 insertions(+), 1 deletion(-)
diff --git a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
index e7ecbed390d537..abd0ea5f90c2d3 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -924,6 +924,13 @@ template <class T, class E> class expected {
expected &operator=(const expected &);
expected &operator=(expected &&) noexcept;
+ template <class U = T>
+ requires(!__is_same(__remove_cvref_t<U>, expected) &&
+ !__is_unexpected<__remove_cvref_t<U>> &&
+ __is_constructible(T, U) && __is_assignable(T &, U))
+ expected &operator=(U &&v);
+
+ template <class G> expected &operator=(const unexpected<G> &e);
template <class G> expected &operator=(unexpected<G> &&e);
const T *operator->() const noexcept;
@@ -994,6 +1001,9 @@ template <class E> class expected<void, E> {
expected &operator=(const expected &);
expected &operator=(expected &&) noexcept;
+ template <class G> expected &operator=(const unexpected<G> &e);
+ template <class G> expected &operator=(unexpected<G> &&e);
+
explicit operator bool() const noexcept;
bool has_value() const noexcept;
diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
index 8e3121f672cebc..417b1f46b09621 100644
--- a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
@@ -1386,16 +1386,230 @@ TEST_P(UncheckedExpectedAccessTest, ConstructNestedUnchecked) {
}
TEST_P(UncheckedExpectedAccessTest, ConstructThenAssignUnexpected) {
- // FIXME: should warn once converting assignments are modeled.
ExpectDiagnosticsFor(R"(
#include "unchecked_expected_access_test.h"
int target() {
$ns::expected<int, int> e;
e = $ns::unexpected(1);
+ return *e; // [[unsafe]]
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, Assign) {
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ e = 5;
+ return *e;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ e = {};
+ return *e;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ if (e)
+ return 0;
+ e = 5;
return *e;
}
)");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ const $ns::unexpected<int> u(1);
+ e = u;
+ return e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ e = $ns::unexpected(1);
+ return e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ return (e = 5).value();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<void, int> e) {
+ e = $ns::unexpected(1);
+ e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+ if (!b)
+ return 0;
+ a = b;
+ return *a;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+ if (!b)
+ return 0;
+ a = std::move(b);
+ return *a;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> a, $ns::expected<long, int> b) {
+ if (!b)
+ return 0;
+ a = b;
+ return *a;
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<$ns::expected<int, int>, int> outer) {
+ $ns::expected<int, int> inner($ns::unexpect, 1);
+ outer = inner;
+ outer.value();
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, AssignMakeUnexpected) {
+ // `make_unexpected` is only provided by `tl`.
+ if (GetParam().NamespaceName != "tl")
+ GTEST_SKIP();
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ e = $ns::make_unexpected(1);
+ return e.error();
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ e = $ns::make_unexpected(1);
+ return *e; // [[unsafe]]
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, AssignUnchecked) {
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ e = $ns::unexpected(1);
+ return *e; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ e = 5;
+ return e.error(); // [[unsafe:error]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e, bool cond) {
+ if (cond)
+ e = 5;
+ return *e; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> e) {
+ return (e = $ns::unexpected(1)).value(); // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+ a = b;
+ return *a; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ int target($ns::expected<int, int> a, $ns::expected<long, int> b) {
+ a = b;
+ return *a; // [[unsafe]]
+ }
+ )");
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<$ns::expected<int, int>, int> outer) {
+ $ns::expected<int, int> inner($ns::unexpect, 1);
+ outer = inner;
+ outer.error(); // [[unsafe:error]]
+ }
+ )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, AssignUncheckedVoid) {
+ // `tl::expected<void, E>` has no `value()`.
+ if (GetParam().NamespaceName != "std")
+ GTEST_SKIP();
+
+ ExpectDiagnosticsFor(R"(
+ #include "unchecked_expected_access_test.h"
+
+ void target($ns::expected<void, int> e) {
+ e = $ns::unexpected(1);
+ e.value(); // [[unsafe]]
+ }
+ )");
}
} // namespace clang::dataflow::test
>From 76ff2efbb028e282ea9fe029a5edf1108227b312 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <valentin.yukhymenko at gmail.com>
Date: Fri, 27 Nov 2026 17:42:58 +0000
Subject: [PATCH 7/7] Model assignments
---
.../Models/UncheckedExpectedAccessModel.cpp | 46 ++++++++++++++++++-
1 file changed, 45 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
index dd4ed03310d823..0706dcc3c42b1b 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
@@ -167,6 +167,22 @@ auto expectedConvertingConstructor() {
hasDeclaration(cxxConstructorDecl(expectedConvertingConstructorDecl())));
}
+// Copy and move assignments are modeled by the framework, which copies the
+// "has_value" property of the source.
+auto expectedAssignment() {
+ return cxxOperatorCallExpr(
+ hasOverloadedOperatorName("="), argumentCountIs(2),
+ callee(cxxMethodDecl(ofClass(expectedClass()),
+ unless(anyOf(isCopyAssignmentOperator(),
+ isMoveAssignmentOperator())))));
+}
+
+// `operator=(const unexpected<G> &)` and `operator=(unexpected<G> &&)`.
+auto expectedErrorAssignment() {
+ return cxxOperatorCallExpr(expectedAssignment(),
+ hasArgument(1, hasUnexpectedType()));
+}
+
/// Ensures that `E` is mapped to a `BoolValue` and returns its formula.
const Formula &forceBoolValue(Environment &Env, const Expr &E) {
auto *Value = Env.get<BoolValue>(E);
@@ -325,7 +341,29 @@ void transferConvertingConstructor(const CXXConstructExpr *E,
constructExpected(*E, *HasValueVal, State.Env);
}
-// FIXME: Model the assignments and `swap`.
+void assignExpected(const CXXOperatorCallExpr &E, BoolValue &HasValueVal,
+ Environment &Env) {
+ auto *Loc = Env.get<RecordStorageLocation>(*E.getArg(0));
+ if (Loc == nullptr)
+ return;
+ setHasValue(Loc, HasValueVal, Env);
+ // The result of the assignment refers to the assigned object.
+ Env.setStorageLocation(E, *Loc);
+}
+
+void transferValueAssignment(const CXXOperatorCallExpr *E,
+ const MatchFinder::MatchResult &,
+ LatticeTransferState &State) {
+ assignExpected(*E, State.Env.getBoolLiteralValue(true), State.Env);
+}
+
+void transferErrorAssignment(const CXXOperatorCallExpr *E,
+ const MatchFinder::MatchResult &,
+ LatticeTransferState &State) {
+ assignExpected(*E, State.Env.getBoolLiteralValue(false), State.Env);
+}
+
+// FIXME: Model `swap`.
auto buildTransferMatchSwitch() {
return CFGMatchSwitchBuilder<LatticeTransferState>()
// expected::has_value, expected::operator bool
@@ -353,6 +391,12 @@ auto buildTransferMatchSwitch() {
transferConvertingConstructor)
.CaseOfCFGStmt<CXXConstructExpr>(expectedConstructor(),
transferValueConstructor)
+ // Assignments. The order matters: the last case matches every
+ // assignment that is not matched by the one before it.
+ .CaseOfCFGStmt<CXXOperatorCallExpr>(expectedErrorAssignment(),
+ transferErrorAssignment)
+ .CaseOfCFGStmt<CXXOperatorCallExpr>(expectedAssignment(),
+ transferValueAssignment)
.Build();
}
More information about the cfe-commits
mailing list