https://github.com/BaLiKfromUA created 
https://github.com/llvm/llvm-project/pull/244872

TODO: description

>From 38c904baa567af7717a6f9eb774d99ef0f71d7f1 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <[email protected]>
Date: Sat, 21 Nov 2026 10:44:33 +0000
Subject: [PATCH 1/5] Basic tests for `operator ==`

---
 .../Analysis/FlowSensitive/MockHeaders.cpp    |  25 +++-
 .../UncheckedExpectedAccessModelTest.cpp      | 113 +++++++++++++++++-
 2 files changed, 135 insertions(+), 3 deletions(-)

diff --git a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp 
b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
index f325b913ae2c7..c1ed5fc2b0f4a 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -898,6 +898,13 @@ template <class T, class E> class expected {
   template <class... Args> T &emplace(Args &&...args) noexcept;
 
   void swap(expected &rhs) noexcept;
+
+  // 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);
+#endif
 };
 
 template <class E> class expected<void, E> {
@@ -938,6 +945,11 @@ template <class E> class expected<void, E> {
   void emplace() noexcept;
 
   void swap(expected &rhs) noexcept;
+
+#if __cplusplus >= 202002L
+  template <class T2, class E2>
+  friend bool operator==(const expected &x, const expected<T2, E2> &y);
+#endif
 };
 
 } // namespace std
@@ -951,7 +963,9 @@ static constexpr char TlExpectedHeader[] = R"(
 
 // Mock of `tl::expected` from https://github.com/TartanLlama/expected.
 //
-// Unlike `std::expected`, it has no `void` specialization.
+// Unlike `std::expected`, it has no `void` specialization and its comparison
+// operators are free functions that are declared for both operand orders,
+// including `operator!=`.
 
 #include "std_type_traits.h"
 
@@ -1081,6 +1095,15 @@ template <class T, class E> class expected {
   template <class U> T value_or(U &&v) &&;
 };
 
+template <class T, class E, class U, class F>
+bool operator==(const expected<T, E> &lhs, const expected<U, F> &rhs);
+template <class T, class E, class U, class F>
+bool operator!=(const expected<T, E> &lhs, const expected<U, F> &rhs);
+template <class E, class F>
+bool operator==(const expected<void, E> &lhs, const expected<void, F> &rhs);
+template <class E, class F>
+bool operator!=(const expected<void, E> &lhs, const expected<void, F> &rhs);
+
 template <class T, class E>
 void swap(expected<T, E> &lhs, expected<T, E> &rhs);
 
diff --git 
a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp 
b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
index 5ef2f3f99db80..282cab1aeac33 100644
--- 
a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
+++ 
b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
@@ -62,8 +62,9 @@ class UncheckedExpectedAccessTest
   //   for an unchecked error access on that line.
   void ExpectDiagnosticsFor(std::string SourceCode) {
     // `tl::expected` is usable since C++17, and `std::expected` since C++23.
-    for (const char *CxxMode : {"-std=c++17", "-std=c++23"})
-      ExpectDiagnosticsFor(SourceCode, CxxMode);
+    if (GetParam().NamespaceName == "tl")
+      ExpectDiagnosticsFor(SourceCode, "-std=c++17");
+    ExpectDiagnosticsFor(SourceCode, "-std=c++23");
   }
 
   void ExpectDiagnosticsFor(std::string SourceCode, const char *CxxMode) {
@@ -610,4 +611,112 @@ TEST_P(UncheckedExpectedAccessTest, 
UnexpectedErrorIsNotAnAccess) {
   )");
 }
 
+TEST_P(UncheckedExpectedAccessTest, EqualToExpected) {
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+      if (a == b && b)
+        return *a;
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+      if (a == b && !b)
+        return a.error();
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+      if (a == b) {
+        if (b)
+          return *a;
+      }
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+      if (a != b)
+        return 0;
+      if (b)
+        return *a;
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> a, $ns::expected<long, int> b) {
+      if (a == b && b)
+        return *a;
+      return 0;
+    }
+  )");
+
+  // `expected<void, E>` objects that both hold a value are equal.
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target($ns::expected<void, int> a, $ns::expected<void, int> b) {
+      if (a != b && a)
+        b.error();
+    }
+  )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, EqualToExpectedUnchecked) {
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+      if (a == b)
+        return *a; // [[unsafe]]
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+      if (a != b && b)
+        return *a; // [[unsafe]]
+      return 0;
+    }
+  )");
+
+  // Unequal expecteds may both hold values that differ.
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+      if (a != b && a)
+        b.error(); // [[unsafe:error]]
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> a, $ns::expected<int, int> b) {
+      if (a == b || b)
+        return *a; // [[unsafe]]
+      return 0;
+    }
+  )");
+}
+
 } // namespace clang::dataflow::test

>From 0484d94f135f96c136e67dc5e0cef55e74a67089 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <[email protected]>
Date: Sat, 21 Nov 2026 10:58:33 +0000
Subject: [PATCH 2/5] Model `operator ==` for comparing expected to other
 expected

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

diff --git 
a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp 
b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
index 1841ebfcaa9a4..648f8b9990adf 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
@@ -13,6 +13,7 @@
 
 #include "clang/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.h"
 #include "clang/AST/DeclCXX.h"
+#include "clang/AST/DeclTemplate.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/ASTMatchers/ASTMatchersMacros.h"
 #include "clang/Analysis/FlowSensitive/Arena.h"
@@ -51,6 +52,18 @@ static bool isExpectedType(QualType Ty) {
   return RD != nullptr && isExpectedClass(*RD);
 }
 
+/// Returns true if `Ty` is a supported expected type whose value type is
+/// `void`, e.g. `std::expected<void, E>`.
+static bool isVoidExpectedType(QualType Ty) {
+  const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(
+      Ty->getAsCXXRecordDecl());
+  if (CTSD == nullptr || !isExpectedClass(*CTSD))
+    return false;
+  const TemplateArgument &ValueType = CTSD->getTemplateArgs()[0];
+  return ValueType.getKind() == TemplateArgument::Type &&
+         ValueType.getAsType()->isVoidType();
+}
+
 namespace {
 
 using namespace ::clang::ast_matchers;
@@ -69,6 +82,45 @@ auto expectedOperatorCall(ArrayRef<StringRef> Names) {
                              callee(cxxMethodDecl(ofClass(expectedClass()))));
 }
 
+auto hasExpectedType() {
+  return hasType(hasUnqualifiedDesugaredType(
+      recordType(hasDeclaration(cxxRecordDecl(expectedClass())))));
+}
+
+/// Matches `operator==` and `operator!=` calls between an expected and an
+/// operand that matches `Other`, in either order.
+auto expectedComparisonCall(
+    const ast_matchers::internal::Matcher<Expr> &Other) {
+  return cxxOperatorCallExpr(
+      hasAnyOverloadedOperatorName("==", "!="), argumentCountIs(2),
+      anyOf(allOf(hasArgument(0, hasExpectedType()), hasArgument(1, Other)),
+            allOf(hasArgument(0, Other), hasArgument(1, hasExpectedType()))));
+}
+
+auto expectedEqualityCall() {
+  return expectedComparisonCall(hasExpectedType());
+}
+
+/// 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);
+  if (Value != nullptr)
+    return Value->formula();
+
+  Value = &Env.makeAtomicBoolValue();
+  Env.setValue(E, *Value);
+  return Value->formula();
+}
+
+/// Returns the formula that is true if and only if the operands of the
+/// `operator==` or `operator!=` call `E` are equal.
+const Formula &equalityFormula(Environment &Env, const CXXOperatorCallExpr &E) 
{
+  const Formula &Result = forceBoolValue(Env, E);
+  if (E.getOperator() == OO_ExclaimEqual)
+    return Env.arena().makeNot(Result);
+  return Result;
+}
+
 StorageLocation &locForHasValue(const RecordStorageLocation &ExpectedLoc) {
   return ExpectedLoc.getSyntheticField("has_value");
 }
@@ -97,6 +149,32 @@ void transferHasValueCall(const CXXMemberCallExpr *E,
     State.Env.setValue(*E, *HasValueVal);
 }
 
+void transferExpectedEqualityCall(const CXXOperatorCallExpr *E,
+                                  const MatchFinder::MatchResult &,
+                                  LatticeTransferState &State) {
+  Environment &Env = State.Env;
+  BoolValue *LHasValueVal =
+      getHasValue(Env, Env.get<RecordStorageLocation>(*E->getArg(0)));
+  BoolValue *RHasValueVal =
+      getHasValue(Env, Env.get<RecordStorageLocation>(*E->getArg(1)));
+  if (LHasValueVal == nullptr || RHasValueVal == nullptr)
+    return;
+
+  Arena &A = Env.arena();
+  const Formula &EqVal = equalityFormula(Env, *E);
+  const Formula &LHasValue = LHasValueVal->formula();
+  const Formula &RHasValue = RHasValueVal->formula();
+
+  // Equal expecteds either both hold a value or both hold an error. Nothing
+  // more follows in general: the values (or errors) themselves may differ.
+  Env.assume(A.makeImplies(EqVal, A.makeEquals(LHasValue, RHasValue)));
+
+  // `expected<void, E>` objects that both hold a value are always equal.
+  if (isVoidExpectedType(E->getArg(0)->getType()) &&
+      isVoidExpectedType(E->getArg(1)->getType()))
+    Env.assume(A.makeImplies(A.makeAnd(LHasValue, RHasValue), EqVal));
+}
+
 // FIXME: Model the constructors, assignments, `emplace` and `swap`.
 auto buildTransferMatchSwitch() {
   return CFGMatchSwitchBuilder<LatticeTransferState>()
@@ -104,6 +182,9 @@ auto buildTransferMatchSwitch() {
       .CaseOfCFGStmt<CXXMemberCallExpr>(
           expectedMemberCall(hasAnyName("has_value", "operator bool")),
           transferHasValueCall)
+      // operator==, operator!= between two expecteds
+      .CaseOfCFGStmt<CXXOperatorCallExpr>(expectedEqualityCall(),
+                                          transferExpectedEqualityCall)
       .Build();
 }
 

>From d3b2b6c51b89d29c399479eaf051d401a7807d57 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <[email protected]>
Date: Sat, 21 Nov 2026 10:59:38 +0000
Subject: [PATCH 3/5] Basic tests for comparing expected with value

---
 .../Analysis/FlowSensitive/MockHeaders.cpp    |  19 +++
 .../UncheckedExpectedAccessModelTest.cpp      | 124 ++++++++++++++++++
 2 files changed, 143 insertions(+)

diff --git a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp 
b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
index c1ed5fc2b0f4a..f4a8ad570645b 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -847,6 +847,12 @@ struct unexpect_t {
 
 inline constexpr unexpect_t unexpect{};
 
+template <class T, class E> class expected;
+
+template <class> constexpr bool __is_expected = false;
+template <class T, class E>
+constexpr bool __is_expected<expected<T, E>> = true;
+
 template <class T, class E> class expected {
 public:
   using value_type = T;
@@ -904,6 +910,10 @@ template <class T, class E> class expected {
 #if __cplusplus >= 202002L
   template <class T2, class E2>
   friend bool operator==(const expected &x, const expected<T2, E2> &y);
+
+  template <class T2>
+    requires(!__is_expected<T2>)
+  friend bool operator==(const expected &x, const T2 &v);
 #endif
 };
 
@@ -1104,6 +1114,15 @@ bool operator==(const expected<void, E> &lhs, const 
expected<void, F> &rhs);
 template <class E, class F>
 bool operator!=(const expected<void, E> &lhs, const expected<void, F> &rhs);
 
+template <class T, class E, class U>
+bool operator==(const expected<T, E> &x, const U &v);
+template <class T, class E, class U>
+bool operator==(const U &v, const expected<T, E> &x);
+template <class T, class E, class U>
+bool operator!=(const expected<T, E> &x, const U &v);
+template <class T, class E, class U>
+bool operator!=(const U &v, const expected<T, E> &x);
+
 template <class T, class E>
 void swap(expected<T, E> &lhs, expected<T, E> &rhs);
 
diff --git 
a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp 
b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
index 282cab1aeac33..1ee0f934fbfdf 100644
--- 
a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
+++ 
b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
@@ -719,4 +719,128 @@ TEST_P(UncheckedExpectedAccessTest, 
EqualToExpectedUnchecked) {
   )");
 }
 
+TEST_P(UncheckedExpectedAccessTest, EqualToValue) {
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e == 5)
+        return *e;
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (5 == e)
+        return e.value();
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e != 5)
+        return 0;
+      return *e;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (5 != e)
+        return 0;
+      return *e;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    long target($ns::expected<long, int> e, int v) {
+      if (e == v)
+        return *e;
+      return 0;
+    }
+  )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, EqualToValueUnchecked) {
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e != 5)
+        return *e; // [[unsafe]]
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (5 != e)
+        return *e; // [[unsafe]]
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e == 5)
+        return 0;
+      return *e; // [[unsafe]]
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (5 == e)
+        return 0;
+      return *e; // [[unsafe]]
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e == 5)
+        return e.error(); // [[unsafe:error]]
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e != 5)
+        return e.error(); // [[unsafe:error]]
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e, bool cond) {
+      if (e == 5 || cond)
+        return *e; // [[unsafe]]
+      return 0;
+    }
+  )");
+}
+
 } // namespace clang::dataflow::test

>From 2f4d4ae9296cbd6f782abc9b187291f68fee4143 Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <[email protected]>
Date: Sat, 21 Nov 2026 11:00:45 +0000
Subject: [PATCH 4/5] Tests for `==` with unexpected

---
 .../Analysis/FlowSensitive/MockHeaders.cpp    |  15 +++
 .../UncheckedExpectedAccessModelTest.cpp      | 108 ++++++++++++++++++
 2 files changed, 123 insertions(+)

diff --git a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp 
b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
index f4a8ad570645b..9e157efa964f9 100644
--- a/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/MockHeaders.cpp
@@ -914,6 +914,9 @@ template <class T, class E> class expected {
   template <class T2>
     requires(!__is_expected<T2>)
   friend bool operator==(const expected &x, const T2 &v);
+
+  template <class E2>
+  friend bool operator==(const expected &x, const unexpected<E2> &e);
 #endif
 };
 
@@ -959,6 +962,9 @@ template <class E> class expected<void, E> {
 #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
 };
 
@@ -1123,6 +1129,15 @@ bool operator!=(const expected<T, E> &x, const U &v);
 template <class T, class E, class U>
 bool operator!=(const U &v, const expected<T, E> &x);
 
+template <class T, class E>
+bool operator==(const expected<T, E> &x, const unexpected<E> &e);
+template <class T, class E>
+bool operator==(const unexpected<E> &e, const expected<T, E> &x);
+template <class T, class E>
+bool operator!=(const expected<T, E> &x, const unexpected<E> &e);
+template <class T, class E>
+bool operator!=(const unexpected<E> &e, const expected<T, E> &x);
+
 template <class T, class E>
 void swap(expected<T, E> &lhs, expected<T, E> &rhs);
 
diff --git 
a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp 
b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
index 1ee0f934fbfdf..550209c73e920 100644
--- 
a/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
+++ 
b/clang/unittests/Analysis/FlowSensitive/UncheckedExpectedAccessModelTest.cpp
@@ -843,4 +843,112 @@ TEST_P(UncheckedExpectedAccessTest, 
EqualToValueUnchecked) {
   )");
 }
 
+TEST_P(UncheckedExpectedAccessTest, EqualToUnexpected) {
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e == $ns::unexpected(1))
+        return e.error();
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if ($ns::unexpected(1) == e)
+        return e.error();
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e != $ns::unexpected(1))
+        return 0;
+      return e.error();
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if ($ns::unexpected(1) != e)
+        return 0;
+      return e.error();
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    void target($ns::expected<void, int> e) {
+      if (e == $ns::unexpected(1))
+        e.error();
+    }
+  )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, EqualToUnexpectedUnchecked) {
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e == $ns::unexpected(1))
+        return *e; // [[unsafe]]
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if ($ns::unexpected(1) == e)
+        return *e; // [[unsafe]]
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e != $ns::unexpected(1))
+        return e.error(); // [[unsafe:error]]
+      return 0;
+    }
+  )");
+
+  ExpectDiagnosticsFor(R"(
+    #include "unchecked_expected_access_test.h"
+
+    int target($ns::expected<int, int> e) {
+      if (e == $ns::unexpected(1))
+        return 0;
+      return e.error(); // [[unsafe:error]]
+    }
+  )");
+}
+
+TEST_P(UncheckedExpectedAccessTest, EqualToUnexpectedUncheckedVoidValue) {
+  // `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) {
+      if (e == $ns::unexpected(1))
+        e.value(); // [[unsafe]]
+    }
+  )");
+}
+
 } // namespace clang::dataflow::test

>From 6dc049cb93fb2eb1c98a53f65249a05d700fd07d Mon Sep 17 00:00:00 2001
From: BaLiKfromUA <[email protected]>
Date: Sat, 21 Nov 2026 11:01:53 +0000
Subject: [PATCH 5/5] Basic implemenation to `==` modeling for value or
 unexpected comparisons

---
 .../Models/UncheckedExpectedAccessModel.cpp   | 60 +++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git 
a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp 
b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
index 648f8b9990adf..b066ea956cbd9 100644
--- a/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Models/UncheckedExpectedAccessModel.cpp
@@ -72,6 +72,11 @@ using LatticeTransferState = TransferState<NoopLattice>;
 
 AST_MATCHER(CXXRecordDecl, expectedClass) { return isExpectedClass(Node); }
 
+AST_MATCHER(CXXRecordDecl, unexpectedClass) {
+  return Node.getDeclName().isIdentifier() && Node.getName() == "unexpected" &&
+         isSupportedNamespace(*Node.getDeclContext());
+}
+
 auto expectedMemberCall(ast_matchers::internal::Matcher<NamedDecl> Name) {
   return cxxMemberCallExpr(
       callee(cxxMethodDecl(Name, ofClass(expectedClass()))));
@@ -87,6 +92,11 @@ auto hasExpectedType() {
       recordType(hasDeclaration(cxxRecordDecl(expectedClass())))));
 }
 
+auto hasUnexpectedType() {
+  return hasType(hasUnqualifiedDesugaredType(
+      recordType(hasDeclaration(cxxRecordDecl(unexpectedClass())))));
+}
+
 /// Matches `operator==` and `operator!=` calls between an expected and an
 /// operand that matches `Other`, in either order.
 auto expectedComparisonCall(
@@ -101,6 +111,15 @@ auto expectedEqualityCall() {
   return expectedComparisonCall(hasExpectedType());
 }
 
+auto expectedValueEqualityCall() {
+  return expectedComparisonCall(
+      unless(anyOf(hasExpectedType(), hasUnexpectedType())));
+}
+
+auto expectedUnexpectedEqualityCall() {
+  return expectedComparisonCall(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);
@@ -121,6 +140,11 @@ const Formula &equalityFormula(Environment &Env, const 
CXXOperatorCallExpr &E) {
   return Result;
 }
 
+/// Returns the expected operand of the comparison `E`, which has exactly one.
+const Expr &getExpectedOperand(const CXXOperatorCallExpr &E) {
+  return isExpectedType(E.getArg(0)->getType()) ? *E.getArg(0) : *E.getArg(1);
+}
+
 StorageLocation &locForHasValue(const RecordStorageLocation &ExpectedLoc) {
   return ExpectedLoc.getSyntheticField("has_value");
 }
@@ -175,6 +199,35 @@ void transferExpectedEqualityCall(const 
CXXOperatorCallExpr *E,
     Env.assume(A.makeImplies(A.makeAnd(LHasValue, RHasValue), EqVal));
 }
 
+void transferExpectedValueEqualityCall(const CXXOperatorCallExpr *E,
+                                       const MatchFinder::MatchResult &,
+                                       LatticeTransferState &State) {
+  Environment &Env = State.Env;
+  BoolValue *HasValueVal =
+      getHasValue(Env, Env.get<RecordStorageLocation>(getExpectedOperand(*E)));
+  if (HasValueVal == nullptr)
+    return;
+
+  // `e == v` --> `e.has_value() && *e == v`
+  Env.assume(Env.arena().makeImplies(equalityFormula(Env, *E),
+                                     HasValueVal->formula()));
+}
+
+void transferExpectedUnexpectedEqualityCall(const CXXOperatorCallExpr *E,
+                                            const MatchFinder::MatchResult &,
+                                            LatticeTransferState &State) {
+  Environment &Env = State.Env;
+  BoolValue *HasValueVal =
+      getHasValue(Env, Env.get<RecordStorageLocation>(getExpectedOperand(*E)));
+  if (HasValueVal == nullptr)
+    return;
+
+  // `e == u` --> `!e.has_value() && e.error() == u.error()`
+  Arena &A = Env.arena();
+  Env.assume(A.makeImplies(equalityFormula(Env, *E),
+                           A.makeNot(HasValueVal->formula())));
+}
+
 // FIXME: Model the constructors, assignments, `emplace` and `swap`.
 auto buildTransferMatchSwitch() {
   return CFGMatchSwitchBuilder<LatticeTransferState>()
@@ -185,6 +238,13 @@ auto buildTransferMatchSwitch() {
       // operator==, operator!= between two expecteds
       .CaseOfCFGStmt<CXXOperatorCallExpr>(expectedEqualityCall(),
                                           transferExpectedEqualityCall)
+      // operator==, operator!= between an expected and a value
+      .CaseOfCFGStmt<CXXOperatorCallExpr>(expectedValueEqualityCall(),
+                                          transferExpectedValueEqualityCall)
+      // operator==, operator!= between an expected and an unexpected
+      .CaseOfCFGStmt<CXXOperatorCallExpr>(
+          expectedUnexpectedEqualityCall(),
+          transferExpectedUnexpectedEqualityCall)
       .Build();
 }
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to