LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 33947 - Wshadow false positives in function-local classes
Summary: Wshadow false positives in function-local classes
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: Frontend (show other bugs)
Version: trunk
Hardware: PC Linux
: P enhancement
Assignee: Alexander Kornienko
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-07-26 04:05 PDT by Alexander Kornienko
Modified: 2017-08-10 18:01 PDT (History)
4 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Kornienko 2017-07-26 04:05:32 PDT
https://godbolt.org/g/54XRMT

void f(int a) {
  struct A {
    void g(int a) {}
    A() { int a; }
  };
}

3 : <source>:3:16: warning: declaration shadows a local variable [-Wshadow]
    void g(int a) {}
               ^
1 : <source>:1:12: note: previous declaration is here
void f(int a) {
           ^
4 : <source>:4:15: warning: declaration shadows a local variable [-Wshadow]
    A() { int a; }
              ^
1 : <source>:1:12: note: previous declaration is here
void f(int a) {
           ^
2 warnings generated.


The local variable `a` of the function `f` can't be accessed from a method of the function-local class A, thus no shadowing occurs and no diagnostic is needed.
Comment 1 Alexander Kornienko 2017-07-27 06:36:09 PDT
BTW, gcc's -Wshadow diagnostic correctly ignores local variables separated by a local class: https://godbolt.org/g/fp7cmx.
Comment 2 Alexander Kornienko 2017-07-27 06:43:11 PDT
Sent https://reviews.llvm.org/D35941 for review.
Comment 3 Arthur O'Dwyer 2017-07-27 11:13:28 PDT
No diagnostic is *ever* "needed" in re shadowing; but this code is certainly confusing and I'm glad Clang gives these two diagnostics. I don't think this is a "false" positive at all: the code is confusing because of shadowing, and I'm glad Clang warns about it so that the programmer can go fix their code.

Of course if Clang were refusing to compile the code, that would be a bug --- it's definitely *legal* code; it's just not *good* code and so it deserves the warnings.
Comment 4 Alexander Kornienko 2017-07-27 18:25:30 PDT
(In reply to Arthur O'Dwyer from comment #3)
> No diagnostic is *ever* "needed" in re shadowing; but this code is certainly
> confusing and I'm glad Clang gives these two diagnostics. I don't think this
> is a "false" positive at all: the code is confusing because of shadowing,
> and I'm glad Clang warns about it so that the programmer can go fix their
> code.
> 
> Of course if Clang were refusing to compile the code, that would be a bug
> --- it's definitely *legal* code; it's just not *good* code and so it
> deserves the warnings.

(cross-posting from https://reviews.llvm.org/D35941)
IIUC, most cases where -Wshadow warnings are issued is when a declaration from an enclosing scope would be accessible if there was no declaration that shadows it. In this case the the local variable of the function would not be accessible inside the local class anyway, so the inner variable with the same name is not confusing (at least, much less confusing than if it would prevent access to the variable of an outer scope). This is close to shadowing of uncaptured locals in lambdas (which -Wshadow doesn't warn about, only -Wshadow-all/-Wshadow-uncaptured-local), but it seems to have even less potential for being misleading. We had a few of requests internally to mute -Wshadow for this case. Another data point is that GCC doesn't warn in this case.

But if I'm overseeing reasons to issue a warning in this case, we could add an analogue of -Wshadow-uncaptured-local for this case. WDYT?
Comment 5 Alexander Kornienko 2017-08-10 07:49:02 PDT
The fix was committed a while ago: https://reviews.llvm.org/rL309569.
Comment 6 Hans Wennborg 2017-08-10 18:01:30 PDT
Merged to 5.0 in r310674.