public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope
@ 2012-11-16 18:16 david at doublewise dot net
  2013-03-05 19:25 ` [Bug c++/55357] " jason at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: david at doublewise dot net @ 2012-11-16 18:16 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55357

             Bug #: 55357
           Summary: -Wshadow warns about lambda function parameters
                    matching variables in outer scope
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: david@doublewise.net


Similar to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30566

input:

int main() {
    int x = 1;
    auto const lambda = [](int x) {
        return x;
    };
}



output:

src/main.cpp: In lambda function:
src/main.cpp:3:30: error: declaration of 'x' shadows a previous local
[-Werror=shadow]
src/main.cpp:2:6: error: shadowed declaration is here [-Werror=shadow]
src/main.cpp: In static member function 'static int
main()::<lambda(int)>::_FUN(int)':
src/main.cpp:5:2: error: declaration of 'x' shadows a previous local
[-Werror=shadow]
src/main.cpp:2:6: error: shadowed declaration is here [-Werror=shadow]
cc1plus: all warnings being treated as errors



My lambda has an empty capture specification, so the outer x is not captured.
Note that if I change the lambda parameter's name but do not change the name of
the returned value, I get an error that "x" was not captured.

I can't decide if this is correct behavior for the warning. It would catch
errors caused by people thinking they were using the outer variables by simply
disallowing overlap, which is good. However, it's not possible to use the outer
scope variable no matter what I name my variables in the inner scope, so there
is no chance of a silent behavior change.

However, the following code should always give a warning with -Wshadow:

int main() {
    int x = 1;
    // Capture everything
    auto const lambda = [&](int x) {
        return x;
    };
}




I also note that gcc warns me about the first line the lambda appears on (line
3) and the last line (line 5).


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-03-26  6:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-16 18:16 [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope david at doublewise dot net
2013-03-05 19:25 ` [Bug c++/55357] " jason at gcc dot gnu.org
2013-03-17  2:39 ` jason at gcc dot gnu.org
2013-03-23 19:22 ` jason at gcc dot gnu.org
2022-03-26  6:54 ` jcl at nvidia dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).