From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5888 invoked by alias); 16 Nov 2012 18:16:55 -0000 Received: (qmail 5741 invoked by uid 48); 16 Nov 2012 18:16:33 -0000 From: "david at doublewise dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/55357] New: -Wshadow warns about lambda function parameters matching variables in outer scope Date: Fri, 16 Nov 2012 18:16:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: david at doublewise dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-11/txt/msg01543.txt.bz2 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()::::_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).