Skip to content

[clang] Add a lifetime_capture_by testcase for temporary capturing object. #117733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/test/Sema/warn-lifetime-analysis-capture-by.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// ****************************************************************************
Expand Down