llvm-project/clang/test/SemaCXX/cxx20-warn-dangling-paren-list-agg-init.cpp
Haojian Wu 26baa00908
[clang] Diagnose dangling references for parenthesized aggregate initialization. (#117690)
Unlike brace initialization, the parenthesized aggregate initialization
in C++20 does not extend the lifetime of a temporary object bound to a
reference in an aggreate. This can lead to dangling references:

```
struct A { const int& r; };
 A a1(1); // well-formed, but results in a dangling reference.
 ``` 

With this patch, clang will diagnose this common dangling issues.

Fixes #101957
2024-11-29 10:15:19 +01:00

60 lines
1.4 KiB
C++

// RUN: %clang_cc1 -verify -std=c++20 %s -fsyntax-only
namespace std {
template <class T> struct remove_reference { typedef T type; };
template <class T> struct remove_reference<T&> { typedef T type; };
template <class T> struct remove_reference<T&&> { typedef T type; };
template <class T> typename remove_reference<T>::type &&move(T &&t);
} // namespace std
// dcl.init 16.6.2.2
struct A {
int a;
int&& r;
};
int f();
int n = 10;
A a1{1, f()}; // OK, lifetime is extended for direct-list-initialization
// well-formed, but dangling reference
A a2(1, f()); // expected-warning {{temporary whose address is used as value}}
// well-formed, but dangling reference
A a4(1.0, 1); // expected-warning {{temporary whose address is used as value}}
A a5(1.0, std::move(n)); // OK
struct B {
const int& r;
};
B test(int local) {
return B(1); // expected-warning {{returning address}}
return B(local); // expected-warning {{address of stack memory}}
}
void f(B b);
void test2(int local) {
// No diagnostic on the following cases where both the aggregate object and
// temporary end at the end of the full expression.
f(B(1));
f(B(local));
}
// Test nested struct.
struct C {
B b;
};
struct D {
C c;
};
C c1(B(
1 // expected-warning {{temporary whose address is used as value}}
));
D d1(C(B(
1 // expected-warning {{temporary whose address is used as value}}
)));