[clang] [LifetimeSafety] Warn on inapplicable `[[clang::lifetimebound]]` when return type can not carry a lifetime (PR #203767)

via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 25 09:19:02 PDT 2026


================
@@ -560,11 +560,23 @@ class LifetimeChecker {
         FDef->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
       return;
 
+    QualType ReturnType = FDef->getReturnType();
+    if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(FDef))
+      ReturnType = AST.getTypeDeclType(cast<TypeDecl>(Ctor->getParent()));
+
+    bool ReturnTypeHasOrigins = FactMgr.getOriginMgr().hasOrigins(ReturnType);
+    if (getImplicitObjectParamLifetimeBoundAttr(FDef) && !ReturnTypeHasOrigins)
+      SemaHelper->reportInapplicableLifetimeboundReturnTy(
+          cast<CXXMethodDecl>(FDef));
+
     for (const auto &PVD : FDef->parameters())
-      if (PVD->hasAttr<LifetimeBoundAttr>() &&
-          !FactMgr.getOriginMgr().hasOrigins(PVD->getType(),
-                                             /*IntrinsicOnly=*/true))
-        SemaHelper->reportInapplicableLifetimebound(PVD);
+      if (PVD->hasAttr<LifetimeBoundAttr>()) {
+        if (!FactMgr.getOriginMgr().hasOrigins(PVD->getType(),
+                                               /*IntrinsicOnly=*/true))
+          SemaHelper->reportInapplicableLifetimebound(PVD, PVD->getType());
+        if (!ReturnTypeHasOrigins && !isa<CXXConstructorDecl>(FDef))
----------------
NeKon69 wrote:

Yeah, this will be pretty noisy; parameters could still escape to fields even if the current object doesn't have origins. I'll use the same `experimental` format for this probably.

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


More information about the cfe-commits mailing list