[clang] [LifetimeSafety] Handle xvalue operand of LValueToRValue cast (PR #192312)

Zhijie Wang via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 15 11:42:04 PDT 2026


https://github.com/aeft created https://github.com/llvm/llvm-project/pull/192312

Under C++23, P2266 wraps the operand of `return p;` in an xvalue NoOp cast for by-value parameters. The `CK_LValueToRValue` branch in FactsGenerator guarded on `!SubExpr->isLValue()`, breaking origin flow and silencing the suggestion for `int* id(int* p) { return p; }`.

Use `isGLValue()`, matching how origins are built and stripped elsewhere in the analysis.

Only add a RUN in suggestion test file, since some tests in `warn-lifetime-safety.cpp` cause a hard error under C++23. For example: `MyObj& f() { MyObj s; return s; }`. `error: non-const lvalue reference to type 'MyObj' cannot bind to a temporary of type 'MyObj'`.

Fixes: #176292

>From ba2af5b642c7bb6b67862d5d3b41e577767f0f6d Mon Sep 17 00:00:00 2001
From: Zhijie Wang <yesterda9 at gmail.com>
Date: Wed, 15 Apr 2026 11:27:42 -0700
Subject: [PATCH] [LifetimeSafety] Handle xvalue operand of LValueToRValue cast

---
 clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp | 3 +--
 clang/test/Sema/warn-lifetime-safety-suggestions.cpp | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index 5adf842e5f6d0..0d92c38bce826 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -288,8 +288,7 @@ void FactsGenerator::VisitCastExpr(const CastExpr *CE) {
 
   switch (CE->getCastKind()) {
   case CK_LValueToRValue:
-    // TODO: Decide what to do for x-values here.
-    if (!SubExpr->isLValue())
+    if (!SubExpr->isGLValue())
       return;
 
     assert(Src && "LValue being cast to RValue has no origin list");
diff --git a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
index d2cf1c175eb57..a1ea1182ee5f0 100644
--- a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
@@ -1,6 +1,7 @@
 // RUN: rm -rf %t
 // RUN: split-file %s %t
 // RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wlifetime-safety -Wno-dangling -I%t -I%S -verify %t/test_source.cpp
+// RUN: %clang_cc1 -fsyntax-only -std=c++23 -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wlifetime-safety -Wno-dangling -I%t -I%S -verify %t/test_source.cpp
 // RUN: %clang_cc1 -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wlifetime-safety -Wno-dangling -I%t -I%S -fixit %t/test_source.cpp
 // RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wno-dangling -I%t -I%S -Werror=lifetime-safety-suggestions %t/test_source.cpp
 



More information about the cfe-commits mailing list