[clang-tools-extra] r351686 - [clang-tidy] misc-non-private-member-variables-in-classes: ignore implicit methods
Miklos Vajna via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 20 06:28:27 PST 2019
Author: vmiklos
Date: Sun Jan 20 06:28:27 2019
New Revision: 351686
URL: http://llvm.org/viewvc/llvm-project?rev=351686&view=rev
Log:
[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.
Modified:
clang-tools-extra/trunk/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp
clang-tools-extra/trunk/docs/clang-tidy/checks/misc-non-private-member-variables-in-classes.rst
clang-tools-extra/trunk/test/clang-tidy/misc-non-private-member-variables-in-classes.cpp
Modified: clang-tools-extra/trunk/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp?rev=351686&r1=351685&r2=351686&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/NonPrivateMemberVariablesInClassesCheck.cpp Sun Jan 20 06:28:27 2019
@@ -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 NonPrivateMemberVariablesInClassesC
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"),
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/misc-non-private-member-variables-in-classes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/misc-non-private-member-variables-in-classes.rst?rev=351686&r1=351685&r2=351686&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/misc-non-private-member-variables-in-classes.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/misc-non-private-member-variables-in-classes.rst Sun Jan 20 06:28:27 2019
@@ -6,11 +6,11 @@ misc-non-private-member-variables-in-cla
`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
-------
Modified: clang-tools-extra/trunk/test/clang-tidy/misc-non-private-member-variables-in-classes.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-non-private-member-variables-in-classes.cpp?rev=351686&r1=351685&r2=351686&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-non-private-member-variables-in-classes.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-non-private-member-variables-in-classes.cpp Sun Jan 20 06:28:27 2019
@@ -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.
More information about the cfe-commits
mailing list