[clang] [clang][Sema] Fix false-positive -Wshadow for friend functions (PR #221249)
ahmed mohamed kamel via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 11 06:36:04 PDT 2026
AhmedKamel10 wrote:
> Careful, I think this fix drops some true-positive -Wshadow warnings as well. Consider this:
>
> ```
> struct Foo {
> int a;
> int x = [this] {
> int a = 0; // shadows Foo::a
> return a;
> }();
> };
> ```
>
> Currently we give a warning about this: https://godbolt.org/z/9xsedberW When I test this example in the PR, the warning is gone.
Yup, I tested that example and the error was indeed gone, thanks for testing that. My original (flawed) implementation assumed that if casting to a `CXXMethodDecl` returned a nullptr, then the function should not fire the warn, but looking at your provided example, I found that casting returned nullptr but the actual type was `CXXRecordDecl`, therefore, it did not fire the warn.
I pushed a fix that distinguishes the two: it only suppresses when the context is a `FunctionDecl` but not a `CXXMethodDecl` (like a free/friend function), and falls through the normal check otherwise. Also added your example in the regression test alongside the friend-function test and both are passing now.
https://github.com/llvm/llvm-project/pull/221249
More information about the cfe-commits
mailing list