[clang] [clang] Don't warn if the capturing object is also temporary. (PR #117733)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 28 00:10:25 PST 2024


https://github.com/hokein updated https://github.com/llvm/llvm-project/pull/117733

>From 30235e403de2130795201b00e2d23e25cdb68baa Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Tue, 26 Nov 2024 17:14:49 +0100
Subject: [PATCH] [clang] Add a lifetime capture testcase for temporary
 capturing object.

---
 .../Sema/warn-lifetime-analysis-capture-by.cpp     | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp b/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
index 4d562bac1e305b..e867296d6d8ea7 100644
--- a/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
@@ -143,6 +143,20 @@ void use() {
 }
 } // namespace this_is_captured
 
+namespace temporary_capturing_object {
+struct S {
+  void add(const int& x [[clang::lifetime_capture_by(this)]]);
+};
+
+void test() {
+  // We still give an warning even the capturing object is a temoprary.
+  // It is possible that the capturing object uses the captured object in its
+  // destructor.
+  S().add(1); // expected-warning {{object whose reference is captured}}
+  S{}.add(1); // expected-warning {{object whose reference is captured}}
+}
+} // namespace ignore_temporary_class_object
+
 // ****************************************************************************
 // Capture by Global and Unknown.
 // ****************************************************************************



More information about the cfe-commits mailing list