llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang][Sema] Invalid -Wshadow diagnostics in friend functions defined inside the class

Open brunodf-gf opened this issue 3 months ago • 3 comments

Clang raises a -Wshadow warning for the function parameter x in this C++ friend function:

class Foo {
    int x;
    friend bool operator==(const Foo& f, int x) {
        return f.x == x;
    }
};

Godbolt link: https://godbolt.org/z/dnr1sEaWM

Practically, I would argue there is no shadowing: the field x is not accessible inside the friend function (if you try to access it, you get "error: invalid use of non-static data member"). Gcc also does not give a warning for this case.

brunodf-gf avatar Sep 04 '26 10:09 brunodf-gf

@llvm/issue-subscribers-clang-frontend

Author: Bruno De Fraine (brunodf-gf)

Clang raises a -Wshadow warning for the function parameter `x` in this C++ friend function:
class Foo {
    int x;
    friend bool operator==(const Foo& f, int x) {
        return f.x == x;
    }
};

Godbolt link: https://godbolt.org/z/dnr1sEaWM

Practically, I would argue there is no shadowing: the field x is not accessible inside the friend function (if you try to access it, you get "error: invalid use of non-static data member"). Gcc also does not give a warning for this case.

Hi @brunodf-gf, I read the issue and did some research and I think I found a good starting point to work on it. Working on the PR rn.

AhmedKamel10 avatar Sep 04 '26 12:09 AhmedKamel10

Created a PR fixing the issue. #221249

AhmedKamel10 avatar Sep 04 '26 15:09 AhmedKamel10