[clang] [LifetimeSafety] Add warning for member functions returning field origins (PR #229784)

via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 12 05:08:12 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-analysis

@llvm/pr-subscribers-clang-temporal-safety

Author: Utkarsh Saxena (usx95)

<details>
<summary>Changes</summary>

This patch introduces an opt-in `-Wlifetime-safety-field-origin-escape` diagnostic in Lifetime Safety to detect non-constructor instance methods whose return value depends on the origin of a reference, pointer, or view field rather than the lifetime of the enclosing object (`*this`). To support this, placeholder loans are issued for origin-carrying fields (`$this.field`) during analysis of instance methods, distinguishing between returning a borrow of a field's storage (which depends on `*this` and warrants `[[clang::lifetimebound]]`) and returning a value derived from a field's origin (where the returned reference or view outlives the wrapper object and instead depends on the underlying referent).

This diagnostic helps identify wrapper/view methods where `[[clang::lifetimebound]]` cannot express the true lifetime contract—annotating such methods with `[[clang::lifetimebound]]` would cause false-positive use-after-scope warnings when a temporary wrapper is destroyed while its underlying referent remains alive, whereas leaving them unannotated risks false negatives when both expire. Because a raw pointer field may represent owned storage tied to `*this` (e.g., `Vector::data()`) rather than a non-owning reference (e.g., `Span::data()`), the warning is suppressed if the method is already marked `[[clang::lifetimebound]]`, and an accompanying note suggests marking the method `[[clang::lifetimebound]]` when the escaping field is a pointer owned by the class.

---
Full diff: https://github.com/llvm/llvm-project/pull/229784.diff


9 Files Affected:

- (modified) clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h (+6) 
- (modified) clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h (+29-14) 
- (modified) clang/include/clang/Basic/DiagnosticGroups.td (+7) 
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+8) 
- (modified) clang/lib/Analysis/LifetimeSafety/Checker.cpp (+29) 
- (modified) clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp (+13-2) 
- (modified) clang/lib/Analysis/LifetimeSafety/Loans.cpp (+16) 
- (modified) clang/lib/Sema/SemaLifetimeSafety.h (+22) 
- (modified) clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp (+102-3) 


``````````diff
The server is unavailable at this time. Please wait a few minutes before you try again.
``````````

</details>


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


More information about the cfe-commits mailing list