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

View File

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

View File

@ -35,6 +35,23 @@ private:
int S1_v3; 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. // All functions are static, do not warn.