False-positive "declaration shadows a field" (cpp:S1117) with friend operator

  • C++
  • cpp:S1117
  • A friend operator does not have this, and commenting out line 8 breaks compilation
  • SonarQube Server
#include <iostream>

class User
{
public:
    friend std::ostream& operator<<(std::ostream& os, const User& user)
    {
        const auto id = user.id;
        os << "User " << id;
        return os;
    }
private:
    const int id = 101;
};

int main()
{
    std::cout << User() << std::endl;
}

Hello @bers,

Thank you for reporting this.

Could you please confirm the exact SonarQube Server version you are using? This is relevant because the CFamily implementation of rule cpp:S1117 was improved in SonarQube Server 2026.1.

One of the improvements specifically addresses cases where a friend function defined within a class has a parameter or local variable with the same name as a non-static class member. Since a friend function is not a member function, it cannot access non-static fields through an implicit this object. The updated rule therefore no longer reports these cases as field shadowing.

Your operator<< example appears to match this scenario. If you are using a version earlier than 2026.1, upgrading may resolve the issue. If you already use 2026.1 or a later version and still see the issue, please share the exact version.

Best regards,
Stevan

We’re on SonarQube IDE v10.2.3.16761, and in the web IDE I see “Data Center Edition v2026.1 (119033)”