Skip to content

Commit 2c242b9

Browse files
authored
[clang] Add a lifetime_capture_by testcase for temporary capturing object. (#117733)
Add a test case to indicate this is an expected behavior.
1 parent f8f238d commit 2c242b9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

clang/test/Sema/warn-lifetime-analysis-capture-by.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ void use() {
143143
}
144144
} // namespace this_is_captured
145145

146+
namespace temporary_capturing_object {
147+
struct S {
148+
void add(const int& x [[clang::lifetime_capture_by(this)]]);
149+
};
150+
151+
void test() {
152+
// We still give an warning even the capturing object is a temoprary.
153+
// It is possible that the capturing object uses the captured object in its
154+
// destructor.
155+
S().add(1); // expected-warning {{object whose reference is captured}}
156+
S{}.add(1); // expected-warning {{object whose reference is captured}}
157+
}
158+
} // namespace ignore_temporary_class_object
159+
146160
// ****************************************************************************
147161
// Capture by Global and Unknown.
148162
// ****************************************************************************

0 commit comments

Comments
 (0)