Search code examples
c++cgcccompiler-warningsredefinition

Is there a GCC flag to emit warning about identical variable redefinition inside scope?


As seen here, the following is valid C code:

int test = 10;
if (true) {
    int test = 10;
}

I'm wondering if there's a flag to warn in cases like that, where the redefinition is identical.


Solution

  • There is: -Wshadow=local. Passing in a different value (instead of "local") also allows more precise control over which identifiers can and can't be shadowed.

    It checks whether the name is the same, which is a good enough approximation.