-
Notifications
You must be signed in to change notification settings - Fork 13k
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
[clang] -Wshadow option produces a warning when explicitly capturing class member using the same name #71976
Comments
This also reproduces with me on clang 15 and 16: struct A {
int a;
void foo() {
auto b = [a = this->a] {
};
}
}; In this scenario, |
CC @Fznamznon |
We're also encountering this in the UnrealEngine codebase as many of our compiles are done with -Wshadow and this has caused hundreds of new warnings. At the very least if this could be put on a separate warning flag that would be really appreciated, for the moment this will block us from updating clang as we don't want to disable -Wshadow entirely to work around it. |
I don't think it is 0fb84bc that is guilty since it adds an orthogonal error message, but I can take a look anyway. |
The new warning appeared due to https://reviews.llvm.org/D124351 . It added a new call to CheckShadow here: llvm-project/clang/lib/Sema/SemaLambda.cpp Line 1155 in 410bf5e
I don't fully understand why it is required. |
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8351,10 +8351,12 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
unsigned WarningDiag = diag::warn_decl_shadow;
SourceLocation CaptureLoc;
- if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC &&
+ if (isa<VarDecl>(D) && NewDC &&
isa<CXXMethodDecl>(NewDC)) {
if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) {
if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
+ if(!isa<VarDecl>(ShadowedDecl))
+ return;
if (RD->getLambdaCaptureDefault() == LCD_None) {
// Try to avoid warnings for lambdas with an explicit capture list.
const auto *LSI = cast<LambdaScopeInfo>(getCurFunction()); I was able to silence the warning with something along the lines of that. |
…field Shadowing warning doesn't make much sense since field is not available in lambda's body without capturing this. Fixes llvm#71976
…field (llvm#74512) Shadowing warning doesn't make much sense since field is not available in lambda's body without capturing this. Fixes llvm#71976
…field (llvm#74512) Shadowing warning doesn't make much sense since field is not available in lambda's body without capturing this. Fixes llvm#71976
as class field Shadowing warning doesn't make much sense since field is not available in lambda's body without capturing this. Fixes llvm/llvm-project#71976
…field (llvm#74512) Shadowing warning doesn't make much sense since field is not available in lambda's body without capturing this. Fixes llvm#71976
…field (llvm#74512) Shadowing warning doesn't make much sense since field is not available in lambda's body without capturing this. Fixes llvm#71976
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:
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 nameb
referring to the lambda member becomes available only when the otherb
is already not. Should it be considered shadowing?Expected behavior: No warning generated
Actual behavior:
warning: declaration shadows a field of 'A' [-Wshadow]
diagnostics producedThe text was updated successfully, but these errors were encountered: