Suggestion for a new checker - Check Public Data Members
Severity: Style This checker is made to find data members which are defined as public in the code and to notify the user about them. Then, the user may consider the option to change their access control to private (if it is actually possible) or to keep the data members public.
Thanks for your contribution.
As already mentioned in another PR please provide unit tests for these checks (positive and negative ones).
This looks like misc-non-private-member-variables-in-classes/cppcoreguidelines-non-private-member-variables-in-classes from clang-tidy i.e. C++ Core Guidelines C.8: Use class rather than struct if any member is non-public and C.9: Minimize exposure of members.
Note: The following is my personal opinion. The project owner and other developers might have a different view on this.
I am not sure how fitting this check is for Cppcheck since this check will generate a lot of noise and might end up being disabled/suppressed as you simply cannot fix these warnings as the code is intentional. Unlike clang-tidy there is no way to configure such checks (I think that's actually a good thing).
Recently clang-tidy has been adding lots of checks which produce a lot of "false positives" as in it just looks for a pattern which might indicate an error but the code almost always intended. (I think these are mainly to satisfy some specific internal requirements and were mainlined for easier maintainability.) That is not what Cppcheck is about IMO. I think in Cppcheck you should be able to enable everything and also fix everything.
We already have such a warning in useStlAlgorithm warning which we don't even fix in out own code base and should probably remove - but that is a different story.
I agree with firewave.
I am not sure if some heuristic can be added to determine if it really should be private.
maybe running this with test-my-pr.py will point out some specific cases where it's extra dangerous to make a member public.
autosar rule M11-0-1 seems to be similar and that is implemented in cppcheck premium