Skip to content

[clang] -Wshadow option produces a warning when explicitly capturing class member using the same name #71976

Closed
@flotsamarch

Description

@flotsamarch

Starting from Clang 17.0.0 (built from source, but this also reproduces on Compiler Explorer) using -Wshadow flag now produces diagnostics for this code:

struct A
{
    int b = 5;

    int foo()
    {
        return [b = b] () 
        {
            return b;
        } ();
    }
};

I couldn't find this change in changelogs and there is no test for this type of capture in warn-shadow-in-lambdas.cpp, so I think this is a bug that was introduced by the commit that fixes #61105.

While technically this might be the case of 'shadowing' (I can't make any references to standard that either support or contradict my opinion), I don't think it has ever been considered a 'shadowing' case which should produce diagnostics and has been a somewhat common C++ pattern.

I don't see much value in that warning, since the name b referring to original class member is not available inside lambda body, and and the name b referring to the lambda member becomes available only when the other b is already not. Should it be considered shadowing?

Expected behavior: No warning generated
Actual behavior: warning: declaration shadows a field of 'A' [-Wshadow] diagnostics produced

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions