https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102263
Bug ID: 102263 Summary: Requesting enhanced warning on returning pointer/reference to local Product: gcc Version: 12.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: barry.revzin at gmail dot com Target Milestone: --- gcc (and clang) both warn on both of these functions: int* a() { int i = 0; return &i; } int& b() { int i = 0; return i; } Which is great. Both of these functions are bad. However, add an extra layer: struct Ptr { int* p; }; struct Ref { int& r; }; Ptr c() { int i = 0; return Ptr{.p=&i}; } Ref d() { int i = 0; return Ref{.r=i}; } And now gcc (nor clang) warn on either of these two functions. But 'c' and 'd' are just as much returning a pointer/reference to a local variable as 'a' and 'b' are, and it would be extremely helpful to warn on these cases. More motivating example from C++20 would be something like this (which came up in discussion of P2415): std::vector<int> get_ints(); auto doubled_ints() { auto ints = get_ints(); return ints | std::views::transform([](int i){ return i * 2; }); } This is returning an object that has a member that has a member that has a pointer to a local variable. Which is bad, this dangles! It would be super cool if such a function got a warning slapped onto it.