https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84544
Bug ID: 84544 Summary: Missing warning when returning a reference to internal variable inside a lambda Product: gcc Version: 7.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jpakkane at gmail dot com Target Milestone: --- Suppose you have a C++14 method like this: auto f1() { int x = 3; auto l = [&]() -> int& { return x; }; return l; } Here the returned lambda contains a reference to the stack allocated integer that goes out of scope. Gcc does not give a warning for this. If you call function f1 like this: int& f2() { auto l = f1(); return l(); } Then Gcc does print the following warning (but only with -O2 or higher): <source>: In function 'int& f2()': <source>:9:14: warning: function returns address of local variable [-Wreturn-local-addr] return l(); ^ <source>:2:9: note: declared here int x = 3; ^ Compiler returned: 0 It would be useful if gcc generated a warning for f1 already.