[clang-tidy] misc-non-private-member-variables-in-classes: ignore implicit methods

Otherwise we don't warn on a struct containing a single public int, but
we warn on a struct containing a single public std::string, which is
inconsistent.

llvm-svn: 351686
This commit is contained in:
Miklos Vajna 2019-01-20 14:28:27 +00:00
parent c934d3a01b
commit 61c818f253
3 changed files with 28 additions and 10 deletions

View File

@ -22,8 +22,8 @@ AST_MATCHER(CXXRecordDecl, hasMethods) {
return std::distance(Node.method_begin(), Node.method_end()) != 0;
}
AST_MATCHER(CXXRecordDecl, hasNonStaticMethod) {
return hasMethod(unless(isStaticStorageClass()))
AST_MATCHER(CXXRecordDecl, hasNonStaticNonImplicitMethod) {
return hasMethod(unless(anyOf(isStaticStorageClass(), isImplicit())))
.matches(Node, Finder, Builder);
}
@ -66,10 +66,11 @@ void NonPrivateMemberVariablesInClassesCheck::registerMatchers(
IgnorePublicMemberVariables ? isProtected() : unless(isPrivate()));
// We only want the records that not only contain the mutable data (non-static
// member variables), but also have some logic (non-static member functions).
// We may optionally ignore records where all the member variables are public.
// member variables), but also have some logic (non-static, non-implicit
// member functions). We may optionally ignore records where all the member
// variables are public.
Finder->addMatcher(cxxRecordDecl(anyOf(isStruct(), isClass()), hasMethods(),
hasNonStaticMethod(),
hasNonStaticNonImplicitMethod(),
unless(ShouldIgnoreRecord),
forEach(InterestingField.bind("field")))
.bind("record"),

View File

@ -6,11 +6,11 @@ misc-non-private-member-variables-in-classes
`cppcoreguidelines-non-private-member-variables-in-classes` redirects here
as an alias for this check.
Finds classes that contain non-static data members in addition to non-static
member functions and diagnose all data members declared with a non-``public``
access specifier. The data members should be declared as ``private`` and
accessed through member functions instead of exposed to derived classes or
class consumers.
Finds classes that contain non-static data members in addition to user-declared
non-static member functions and diagnose all data members declared with a
non-``public`` access specifier. The data members should be declared as
``private`` and accessed through member functions instead of exposed to derived
classes or class consumers.
Options
-------

View File

@ -35,6 +35,23 @@ private:
int S1_v3;
};
// Only data and implicit or static methods, do not warn
class C {
public:
C() {}
~C() {}
};
struct S1Implicit {
C S1Implicit_v0;
};
struct S1ImplicitAndStatic {
C S1Implicit_v0;
static void s() {}
};
//----------------------------------------------------------------------------//
// All functions are static, do not warn.