================
@@ -2896,6 +2897,73 @@ TEST_P(UncheckedOptionalAccessTest, 
DiagnosticsHaveRanges) {
   )cc");
 }
 
+TEST_P(UncheckedOptionalAccessTest, AssertTrueGtestMacro) {
+  ExpectDiagnosticsFor(R"cc(
+    #include "unchecked_optional_access_test.h"
+
+    void target() {
+       $ns::$optional<int> opt;
+       ASSERT_TRUE(opt.has_value());
+       EXPECT_EQ(opt.value(), 42);
+
+       opt.reset();
+       EXPECT_EQ(opt.value(), 42); // [[unsafe]]
+       ASSERT_TRUE(opt.value()); // [[unsafe]]
+    }
+  )cc");
+
+  ExpectDiagnosticsFor(R"cc(
+    #include "unchecked_optional_access_test.h"
+
+    void target() {
+       $ns::$optional<int> opt;
+       ASSERT_TRUE(opt);
+       EXPECT_EQ(*opt, 42);
+    }
+  )cc");
+
+  ExpectDiagnosticsFor(R"cc(
+    #include "unchecked_optional_access_test.h"
+
+    void target() {
+       $ns::$optional<int> opt;
+       ASSERT_FALSE(opt.has_value());
+       EXPECT_EQ(opt.value(), 42); // [[unsafe]]
+    }
+  )cc");
+
+  ExpectDiagnosticsFor(R"cc(
+    #include "unchecked_optional_access_test.h"
+
+    void target() {
+       $ns::$optional<int> opt;
+       EXPECT_TRUE(opt.has_value());
+       EXPECT_EQ(opt.value(), 42); // [[unsafe]]
+
+       EXPECT_TRUE(opt);
+       EXPECT_EQ(*opt, 42); // [[unsafe]]
+    }
+  )cc");
+
+  ExpectDiagnosticsFor(R"cc(
+    #include "unchecked_optional_access_test.h"
+
+    namespace BloombergLP::bdlb {
+      template <typename T>
+      struct NullableValue : $ns::$optional<T> {
+       bool isNull() const;
+      };
+    }
+
+    void target() {
+       BloombergLP::bdlb::NullableValue<int> opt;
+
+       ASSERT_FALSE(opt.isNull());
----------------
jvoung wrote:

This could be okay, but I think it would be better to split this out of 
`TEST_P(UncheckedOptionalAccessTest, AssertTrueGtestMacro)` into its own test 
case at least.

Also, note that `TEST_P(UncheckedOptionalAccessTest, ...` is a parameterized 
test, so will run 3 times to iterate through `std::optional` vs 
`absl::optional` vs `base::Optional` (identical API variants), but maybe it is 
fast enough that it is a minor issue.

Also, it actually looks like:

- `base::Optional` from Chromium was replaced with `absl::optional` 
https://source.chromium.org/chromium/chromium/src/+/7dd58adeb690920a7474b4f7c5d71d23b15d4698
 and 
https://docs.google.com/document/d/1xK8E9HyNJW8_zHNLOwFZC5cU9QJj6sK7gXkYCMjbrBQ/edit?tab=t.0

- and then `absl::optional` is also now removed 
https://github.com/abseil/abseil-cpp/commit/22b1f421fa678434722202f2a883a565b7de5343
 (about a year ago), but LTS window is 2 years, so that could wait a bit more. 
Filed https://github.com/llvm/llvm-project/issues/187013 to track as a separate 
issue.


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

Reply via email to