[clang] [clang-tidy] [dataflow] Cache reference accessors for `bugprone-unchecked-optional-access` (PR #128437)
Valentyn Yukhymenko via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 23 13:54:33 PST 2025
================
@@ -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
More information about the cfe-commits
mailing list