[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 14:26:51 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/8] 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 b4dc2fb6bd0fc6fb2a8a63431644a0a26f3228e5 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/8] support `emplace` for basic scenarios

---
 .../Models/UncheckedExpectedAccessModel.cpp   | 21 ++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
index f0d5f3764f028a..c1ab1a3b496822 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
@@ -170,6 +170,15 @@ 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`.
+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 +187,13 @@ void transferHasValueCall(const CXXMemberCallExpr *E,
     State.Env.setValue(*E, *HasValueVal);
 }
 
+void transferEmplaceCall(const CXXMemberCallExpr *E,
+                         const MatchFinder::MatchResult &,
+                         LatticeTransferState &State) {
+  setHasValue(getImplicitObjectLocation(*E, State.Env),
+              State.Env.getBoolLiteralValue(true), State.Env);
+}
+
 void transferExpectedEqualityCall(const CXXOperatorCallExpr *E,
                                   const MatchFinder::MatchResult &,
                                   LatticeTransferState &State) {
@@ -233,13 +249,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 03668e0e7ac0de1c182ccdb9762501bd92611b42 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/8] Tests for constructors

---
 .../Analysis/FlowSensitive/MockHeaders.cpp    |  47 ++-
 .../UncheckedExpectedAccessModelTest.cpp      | 273 ++++++++++++++++++
 2 files changed, 315 insertions(+), 5 deletions(-)

diff --git a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
index 9e157efa964f91..4f6a7386df703c 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -824,6 +824,12 @@ 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"
+
+// Uses C++20 features, but is also included by the `tl` tests in C++17 mode.
+#if __cplusplus >= 202002L
+
 namespace std {
 
 template <class E> class unexpected {
@@ -853,6 +859,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 +885,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 +941,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 +950,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 +962,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 +996,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 758d1b43a751563e0369cc2aea7a788a287d5f0c 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/8] 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 4f6a7386df703c..bdc8f5f2ed895c 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -875,6 +875,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;
@@ -885,14 +894,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);
@@ -1058,6 +1074,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 {
@@ -1080,12 +1105,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 353496bd16146e12f25ba3d7c1e824dede4ac0cb 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/8] model constructors of std::expected

---
 .../Models/UncheckedExpectedAccessModel.cpp   | 81 ++++++++++++++++++-
 1 file changed, 80 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
index c1ab1a3b496822..454f70d13d2c8d 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
@@ -120,6 +120,50 @@ auto expectedUnexpectedEqualityCall() {
   return expectedComparisonCall(hasUnexpectedType());
 }
 
+// Copy and move constructors are handled by the framework.
+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())));
+}
+
+// `expected(const expected<U, G> &)` and `expected(expected<U, G> &&)`,
+// matched by declaration since `expected(U &&)` may also take an `expected`.
+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);
@@ -249,7 +293,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
@@ -269,6 +339,15 @@ auto buildTransferMatchSwitch() {
       .CaseOfCFGStmt<CXXOperatorCallExpr>(
           expectedUnexpectedEqualityCall(),
           transferExpectedUnexpectedEqualityCall)
+      // expected::expected (unexpect_t, unexpected)
+      .CaseOfCFGStmt<CXXConstructExpr>(expectedErrorConstructor(),
+                                       transferErrorConstructor)
+      // expected::expected (converting)
+      .CaseOfCFGStmt<CXXConstructExpr>(expectedConvertingConstructor(),
+                                       transferConvertingConstructor)
+      // expected::expected (value, default, in place)
+      .CaseOfCFGStmt<CXXConstructExpr>(expectedConstructor(),
+                                       transferValueConstructor)
       .Build();
 }
 

>From f1dd7818ca19bf122b18999d958b63e0b923bda8 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/8] 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 bdc8f5f2ed895c..11a19498c86f21 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -922,6 +922,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;
@@ -992,6 +999,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 d40b7eaf8cfea5fc43610a59970d328baa088c88 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/8] Model assignments

---
 .../Models/UncheckedExpectedAccessModel.cpp   | 45 ++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
index 454f70d13d2c8d..75ce8a344310e2 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
@@ -164,6 +164,21 @@ auto expectedConvertingConstructor() {
       hasDeclaration(cxxConstructorDecl(expectedConvertingConstructorDecl())));
 }
 
+// Copy and move assignments are handled by the framework.
+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);
@@ -319,7 +334,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);
+  // Assign a storage location for the whole expression.
+  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
@@ -348,6 +385,12 @@ auto buildTransferMatchSwitch() {
       // expected::expected (value, default, in place)
       .CaseOfCFGStmt<CXXConstructExpr>(expectedConstructor(),
                                        transferValueConstructor)
+      // expected::operator= (unexpected)
+      .CaseOfCFGStmt<CXXOperatorCallExpr>(expectedErrorAssignment(),
+                                          transferErrorAssignment)
+      // expected::operator= (value)
+      .CaseOfCFGStmt<CXXOperatorCallExpr>(expectedAssignment(),
+                                          transferValueAssignment)
       .Build();
 }
 

>From ff52cab7410038aa67dcf69aadaff7a2e267d9fc Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <valentin.yukhymenko at gmail.com>
Date: Sun, 29 Nov 2026 21:48:11 +0000
Subject: [PATCH 8/8] Follow the bool special case of std::expected in the mock

---
 .../Analysis/FlowSensitive/MockHeaders.cpp    | 17 ++--
 .../UncheckedExpectedAccessModelTest.cpp      | 77 +++++++++++++++++++
 2 files changed, 88 insertions(+), 6 deletions(-)

diff --git a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
index 11a19498c86f21..f0f76918e4f777 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -876,13 +876,15 @@ 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.
+// converting constructors from `expected<U, G>` are disabled. As in the
+// standard, `bool` is an exception.
 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>);
+    !__is_same(__remove_cvref_t<T>, bool) &&
+    (__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:
@@ -908,7 +910,10 @@ template <class T, class E> class expected {
     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_constructible(T, U))
+             !__is_unexpected<__remove_cvref_t<U>> &&
+             __is_constructible(T, U) &&
+             (!__is_same(__remove_cvref_t<T>, bool) ||
+              !__is_expected<__remove_cvref_t<U>>))
   expected(U &&v);
 
   template <class G> expected(const unexpected<G> &e);
diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
index 417b1f46b09621..b65ec497a1e45f 100644
--- a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
@@ -1385,6 +1385,83 @@ TEST_P(UncheckedExpectedAccessTest, ConstructNestedUnchecked) {
   )");
 }
 
+// `std::expected<bool, E>` is built from another `std::expected` by the
+// converting constructor, not from the value `bool(e)`.
+TEST_P(UncheckedExpectedAccessTest, ConstructBoolFromExpected) {
+  // `tl::expected<bool, E>` is built from `bool(e)`.
+  if (GetParam().NamespaceName != "std")
+    GTEST_SKIP();
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target(std::expected<int, int> src) {
+      if (!src)
+        return;
+      std::expected<bool, long> dst(src);
+      *dst;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target(std::expected<int, int> src) {
+      if (!src)
+        return;
+      std::expected<bool, long> dst(std::move(src));
+      *dst;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target(std::expected<int, int> src) {
+      std::expected<bool, long> dst;
+      if (!src)
+        return;
+      dst = src;
+      *dst;
+    }
+  )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, ConstructBoolFromExpectedUnchecked) {
+  // `tl::expected<bool, E>` is built from `bool(e)`.
+  if (GetParam().NamespaceName != "std")
+    GTEST_SKIP();
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target(std::expected<int, int> src) {
+      std::expected<bool, long> dst(src);
+      *dst; // [[unsafe]]
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target() {
+      std::expected<int, int> src(std::unexpect, 1);
+      std::expected<bool, long> dst(src);
+      dst.value(); // [[unsafe]]
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target(std::expected<int, int> src) {
+      std::expected<bool, long> dst;
+      dst = src;
+      *dst; // [[unsafe]]
+    }
+  )");
+}
+
 TEST_P(UncheckedExpectedAccessTest, ConstructThenAssignUnexpected) {
   ExpectDiagnosticsFor(R"(
     #include "unchecked_expected_access_test.h"



More information about the cfe-commits mailing list