[clang] [LifetimeSafety] Warn on incorrectly placed `[[clang::lifetimebound]]` attributes (PR #196144)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Sat May 9 00:17:18 PDT 2026


================
@@ -137,6 +139,8 @@ class LifetimeChecker {
         else if (auto *FieldEsc = dyn_cast<FieldEscapeFact>(OEF);
                  FieldEsc && isa<CXXConstructorDecl>(FD))
           AnnotationWarningsMap.try_emplace(PVD, FieldEsc->getFieldDecl());
+      } else {
+        VerifiedLiftimeboundEscapes.insert(PVD);
       }
----------------
usx95 wrote:

nit: consider inverting the condition to remove the negation:

```cpp
if (PVD->hasAttr<LifetimeBoundAttr>()) {
  // Track that this lifetimebound parameter correctly escapes through return.
  VerifiedLiftimeboundEscapes.insert(PVD);
} else {
  // Otherwise, suggest lifetimebound for parameter escaping through return
  // or a field in constructor.
  if (auto *ReturnEsc = dyn_cast<ReturnEscapeFact>(OEF))
    AnnotationWarningsMap.try_emplace(PVD, ReturnEsc->getReturnExpr());
  else if (auto *FieldEsc = dyn_cast<FieldEscapeFact>(OEF);
           FieldEsc && isa<CXXConstructorDecl>(FD))
    AnnotationWarningsMap.try_emplace(PVD, FieldEsc->getFieldDecl());
}
```

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


More information about the cfe-commits mailing list