Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wshadow false positives in function-local classes #33294

Closed
llvmbot opened this issue Jul 26, 2017 · 6 comments
Closed

Wshadow false positives in function-local classes #33294

llvmbot opened this issue Jul 26, 2017 · 6 comments
Labels
bugzilla Issues migrated from bugzilla clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 26, 2017

Bugzilla Link 33947
Resolution FIXED
Resolved on Aug 10, 2017 18:01
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @hyp,@Quuxplusone,@zmodem

Extended Description

https://godbolt.org/g/54XRMT

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

3 : :3:16: warning: declaration shadows a local variable [-Wshadow]
void g(int a) {}
^
1 : :1:12: note: previous declaration is here
void f(int a) {
^
4 : :4:15: warning: declaration shadows a local variable [-Wshadow]
A() { int a; }
^
1 : :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.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Jul 27, 2017

BTW, gcc's -Wshadow diagnostic correctly ignores local variables separated by a local class: https://godbolt.org/g/fp7cmx.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Jul 27, 2017

Sent https://reviews.llvm.org/D35941 for review.

@Quuxplusone
Copy link
Contributor

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.

@llvmbot
Copy link
Collaborator Author

llvmbot commented Jul 28, 2017

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?

@llvmbot
Copy link
Collaborator Author

llvmbot commented Aug 10, 2017

The fix was committed a while ago: https://reviews.llvm.org/rL309569.

@zmodem
Copy link
Collaborator

zmodem commented Aug 11, 2017

Merged to 5.0 in r310674.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

3 participants