Bug 90647 - Warn on returning a lambda with captured local variables
Summary: Warn on returning a lambda with captured local variables
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: lambdas Wreturn-local-addr
  Show dependency treegraph
 
Reported: 2019-05-27 17:30 UTC by Antony Polukhin
Modified: 2019-11-30 18:28 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-11-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Antony Polukhin 2019-05-27 17:30:56 UTC
Consider the example:

auto test(int s) {
    return [&s] { return s; };
}


`s` is a local variable, so we return a lambda that has a dangling reference.

It would be nice to have a warning for such cases.
Comment 1 Eric Gallager 2019-11-30 04:48:28 UTC
(In reply to Antony Polukhin from comment #0)
> Consider the example:
> 
> auto test(int s) {
>     return [&s] { return s; };
> }
> 
> 
> `s` is a local variable, so we return a lambda that has a dangling reference.
> 
> It would be nice to have a warning for such cases.

Under -Wreturn-local-addr, or a new flag?
Comment 2 Antony Polukhin 2019-11-30 08:04:22 UTC
-Wreturn-local-addr looks good to me
Comment 3 Eric Gallager 2019-11-30 10:52:56 UTC
(In reply to Antony Polukhin from comment #2)
> -Wreturn-local-addr looks good to me

Hm, I suddenly got the idea that there was another bug under the -Wreturn-local-addr meta-bug that this might be a dup of, but after checking, it doesn't look like it after all... confirmed as a separate bug for now, I guess.
Comment 4 Eric Gallager 2019-11-30 18:28:23 UTC
(In reply to Eric Gallager from comment #3)
> (In reply to Antony Polukhin from comment #2)
> > -Wreturn-local-addr looks good to me
> 
> Hm, I suddenly got the idea that there was another bug under the
> -Wreturn-local-addr meta-bug that this might be a dup of, but after
> checking, it doesn't look like it after all... confirmed as a separate bug
> for now, I guess.

I guess it might have been bug 70692 that I was thinking of? But that's a different -Wreturn-local-addr issue; it's only related enough for the "See Also" field...