Commit eb74b596 authored by Hans Wennborg's avatar Hans Wennborg
Browse files

Merging r351686:

------------------------------------------------------------------------
r351686 | vmiklos | 2019-01-20 15:28:27 +0100 (Sun, 20 Jan 2019) | 5 lines

[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: 351844
parent 119d8a51
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -23,8 +23,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);
}

@@ -67,10 +67,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"),
+5 −5
Original line number Diff line number Diff line
@@ -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
-------
+17 −0
Original line number Diff line number Diff line
@@ -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.