Bug 79328 - Wshadow and lambda captures
Summary: Wshadow and lambda captures
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda, diagnostic
Depends on:
Blocks: Wshadow lambdas
  Show dependency treegraph
 
Reported: 2017-02-01 16:52 UTC by Nathan Sidwell
Modified: 2025-09-22 02:12 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
example (224 bytes, text/plain)
2017-02-01 16:52 UTC, Nathan Sidwell
Details
simple test case (130 bytes, text/x-csrc)
2019-08-02 08:18 UTC, Andrea Bocci
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Sidwell 2017-02-01 16:52:56 UTC
Created attachment 40647 [details]
example

-Wshadow warnings have a confusing interaction with lambda captures.
1) The names of init lambda captures are not checked.
2) local names are checked regardless of whether there is a default capture to make an outer object accessible

The original report also mentioned the following case:
T x;
[=, x = std::move(x)] {};
if T is a non-copyable type, the default = capture couldn't capture it.

IMHO such nuance shouldn't affect whether or not this gives a warning (so it should warn).

I think it clear that #1 should be checked, but I can see arguments for and against #2.  I think of name hiding as (almost) a syntactic check, and whether an object can be captured is more of a semantic check.
Comment 1 Andrea Bocci 2019-08-02 08:18:03 UTC
If I may, I think at least this case


  // Warning considered erroneous
  // There is no default capture to get at the outer object
  void haveWarningBad (int x) {
    [] { int x; };
  }


should be fixed not to produce any warnings.
Comment 2 Andrea Bocci 2019-08-02 08:18:58 UTC
Created attachment 46658 [details]
simple test case

Build with

g++-9 test.cc -Wshadow -o test
Comment 3 Arnaud Desitter 2019-09-27 15:13:25 UTC
This defect makes -Wshadow=local difficult to use with immediately invoked lambda expressions. For instance,
gcc 9.2 compiles:
#include <array>
auto f(int s) {
  const auto v = []{
      std::array<int,2> v = {1, 2};
      return v;
  }();
  return v.size();
}
with:
source>: In lambda function:

<source>:4:25: warning: declaration of 'v' shadows a previous local [-Wshadow]

    4 |       std::array<int,2> v = {1, 2};

      |                         ^

<source>:3:14: note: shadowed declaration is here

    3 |   const auto v = []{

      |              ^
Comment 4 Lucas Clemente Vella 2021-05-27 17:57:17 UTC
Can confirm this also happens with gcc 10.2.0.
Comment 5 Rolf Eike Beer 2024-08-15 07:16:31 UTC
still in 13.3
Comment 6 Pavel Novikov 2025-09-21 13:20:44 UTC
Still not fixed in 15.2.
https://godbolt.org/z/zeed4KhM1