Base class type or static member accessed through Derived class

class Base
{
    enum Type 
    {
        First,
        Second,
    };
};

class Derived : public Base
{
};

void function()
{
    auto type = Derived::Type::First;
}

Accessing a member of a base class through a derived type is confusing and leads to wrong assumptions about which header file you need to include.

This rule is similar to the clang tidy rule about accessing static class members through an instance of said class https://clang.llvm.org/extra/clang-tidy/checks/readability/static-accessed-through-instance.html

Hello @torgeir.skogen,

Thank you for your insight! I have created a ticket to handle this: CPP-4550 Proposed Rule: Base class types should not be accessed via derived classes.

We already have a rule for static members: S3252: “static” base class members should not be accessed via derived types. This rule is not part of SonarWay because fixing it may not be the cleanest approach: for example, we can’t expect people that use a static variable defined in a library to know how the library is implemented.

Best,
Amélie