[clang] [LifetimeSafety] Warn on inapplicable [[clang::lifetimebound]] parameters (PR #201101)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 10 05:11:19 PDT 2026


================
@@ -686,9 +693,10 @@ Detects misuse of [[clang::noescape]] annotation where the parameter escapes (fo
 }
 
 def LifetimeSafetyValidations : DiagGroup<"lifetime-safety-validations",
-                                          [LifetimeSafetyNoescape,
-                                           LifetimeSafetyLifetimeboundViolation,
-                                           LifetimeSafetyMisplacedLifetimebound]> {
+                                         [LifetimeSafetyNoescape,
+                                          LifetimeSafetyLifetimeboundViolation,
+                                          LifetimeSafetyInapplicableLifetimebound,
+                                          LifetimeSafetyMisplacedLifetimebound]> {
----------------
usx95 wrote:

I think these should be broken into two separate categories. `LifetimeSafetyMisplacedLifetimebound` and `LifetimeSafetyInapplicableLifetimebound` do not require CFG-based analysis.

High-level suggestion (AI)
```cpp
def LifetimeSafetyAnnotationPlacement
    : DiagGroup<"lifetime-safety-annotation-placement",
                [LifetimeSafetyInapplicableLifetimebound,
                 LifetimeSafetyMisplacedLifetimebound]> {
  code Documentation = [{
Validates that lifetime annotations are placed on appropriate types and in appropriate locations.
  }];
}

def LifetimeSafetyAnnotationVerification
    : DiagGroup<"lifetime-safety-annotation-verification",
                [LifetimeSafetyNoescape,
                 LifetimeSafetyLifetimeboundViolation]> {
  code Documentation = [{
Verifies that function implementations adhere to their lifetime annotation contracts through dataflow analysis.
  }];
}

def LifetimeSafetyValidations : DiagGroup<"lifetime-safety-validations",
                                         [LifetimeSafetyAnnotationPlacement,
                                          LifetimeSafetyAnnotationVerification]> {
  code Documentation = [{
Validates correct usage and adherence to lifetime safety annotations like [[clang::noescape]] and [[clang::lifetimebound]].
  }];
}
```

You can use `-Wlifetime-safety-annotation-placement` in the tests. One invocation should be fine as it does not depend on the CFG.

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


More information about the cfe-commits mailing list