[clang] [clang][Sema] Diagnose dangling view field from owner parameter (PR #195981)

Chen Miao via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 26 00:44:48 PDT 2026


================
@@ -1288,8 +1288,11 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
         // Suppress false positives for code like the one below:
         //   Ctor(unique_ptr<T> up) : pointer(up.get()), owner(move(up)) {}
         // FIXME: move this logic to analyzePathForGSLPointer.
-        if (DRE && isGslOwnerType(DRE->getType()))
-          return false;
+        if (DRE && isGslOwnerType(DRE->getType())) {
+          auto *Member = ExtendingEntity ? ExtendingEntity->getDecl() : nullptr;
+          if (!Member || !isGslPointerType(Member->getType()))
----------------
ChenMiaoi wrote:

``` cc
struct Safe {
  std::span<int> view;
  std::vector<int> owner;

  Safe(std::vector<int> v)
      : view(v), owner(std::move(v)) {}
};
```

`std::span<int>` is a GSL pointer type, so this change also affects the example above. However, moving v into owner preserves pointers to its elements, so view remains valid after the constructor returns. I tested this locally and found that the patch introduces a false positive for this case.

https://github.com/llvm/llvm-project/pull/195981


More information about the cfe-commits mailing list