================
@@ -3863,6 +3863,192 @@ TEST_P(UncheckedOptionalAccessTest,
ConstBoolAccessorWithModInBetween) {
)cc");
}
+TEST_P(UncheckedOptionalAccessTest,
+ ConstRefAccessorToOptionalViaConstRefAccessorToHoldingObject) {
+ ExpectDiagnosticsFor(R"cc(
+ #include "unchecked_optional_access_test.h"
+
+ class A {
+ public:
+ const $ns::$optional<int>& get() const { return x; }
+
+ private:
+ $ns::$optional<int> x;
+ };
+
+ class B {
+ public:
+ const A& getA() const { return a; }
+
+ private:
+ A a;
+ };
+
+ void target(B& b) {
+ if (b.getA().get().has_value()) {
+ b.getA().get().value();
+ }
+ }
+ )cc");
+}
+
+TEST_P(
+ UncheckedOptionalAccessTest,
+
ConstRefAccessorToOptionalViaConstRefAccessorToHoldingObjectWithoutValueCheck) {
+ ExpectDiagnosticsFor(R"cc(
+ #include "unchecked_optional_access_test.h"
+
+ class A {
+ public:
+ const $ns::$optional<int>& get() const { return x; }
+
+ private:
+ $ns::$optional<int> x;
+ };
+
+ class B {
+ public:
+ const A& getA() const { return a; }
+
+ private:
+ A a;
+ };
+
+ void target(B& b) {
+ b.getA().get().value(); // [[unsafe]]
+ }
+ )cc");
+}
+
+TEST_P(UncheckedOptionalAccessTest,
+ ConstRefToOptionalSavedAsTemporaryVariable) {
----------------
BaLiKfromUA wrote:
added this test to check that the existing workaround still works. maybe
redundant test
https://github.com/llvm/llvm-project/pull/128437
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits