================ @@ -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) { + 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) { + const auto& opt = b.getA().get(); + if (opt.has_value()) { + opt.value(); + } + } + )cc"); +} + +TEST_P(UncheckedOptionalAccessTest, + ConstRefAccessorToOptionalViaConstValueAccessorToHoldingObject) { ---------------- jvoung wrote:
Maybe "ByValue" to be consistent with an earlier test name https://github.com/llvm/llvm-project/pull/128437 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits