Skip to content

Latest commit

 

History

History
438 lines (236 loc) · 25.7 KB

classes_hierarchy.md

File metadata and controls

438 lines (236 loc) · 25.7 KB
  • Look for structs with all data private and classes with public members.

no enforcement

  • Look for non-virtual member functions that do not touch data members directly. The snag is that many member functions that do not need to touch data members directly do.
  • Ignore virtual functions.
  • Ignore functions that are part of an overload set out of which at least one function accesses private members.
  • Ignore functions returning this.

no enforcement

  • Flag global functions taking argument types from a single namespace.

no enforcement

  • Flag if the } of a class or enumeration definition is not followed by a ;. The ; is missing.

no enforcement

  • Flag classes declared with struct if there is a private or protected member.

no enforcement, see C.2

  • Flag protected data.
  • Flag mixtures of public and private data

no enforcement

  • (Simple) A class should have a declaration (even a =delete one) for either all or none of the special functions)

clang-tidy: cppcoreguidelines-cppcoreguidelines-special-member-functions

core-check: C26432 DEFINE_OR_DELETE_SPECIAL_OPS

  • (Complex) A copy/move constructor and the corresponding copy/move assignment operator should write to the same member variables at the same level of dereference.
  • (Complex) Any member variables written in a copy/move constructor should also be initialized by all other constructors.
  • (Complex) If a copy/move constructor performs a deep copy of a member variable, then the destructor should modify the member variable.
  • (Complex) If a destructor is modifying a member variable, that member variable should be written in any copy/move constructors or assignment operators.

no enforcement

  • Look for likely "implicit resources", such as pointers and references. Look for classes with destructors even though all their data members have destructors.

clang-tidy: cppcoreguidelines-owning-memory

  • (Simple) If a class has pointer or reference member variables that are owners (e.g., deemed owners by using gsl::owner), then they should be referenced in its destructor.
  • (Hard) Determine if pointer or reference member variables are owners when there is no explicit statement of ownership (e.g., look into the constructors).

clang-tidy: cppcoreguidelines-owning-memory
TODO: the check only ensures the existence of a destructor, but not if the member is used

  • Look at the initialization of raw member pointers and member references and see if an allocation is used.

clang-tidy: cppcoreguidelines-owning-memory

  • A class with a pointer data member is suspect.
  • A class with an owner should define its default operations.

clang-tidy: cppcoreguidelines-owning-memory,cppcoreguidelines-special-member-functions

  • A class with any virtual functions should have a destructor that is either public and virtual or else protected and nonvirtual.

core-check: C26436 NEED_VIRTUAL_DTOR

  • (Simple) A destructor should be declared noexcept if it could throw.

no enforcement, but there is something in the pipeline

  • (Simple) A destructor should be declared noexcept if it could throw.

no enforcement, same as C.36

  • Flag classes with user-defined copy operations but no constructor (a user-defined copy is a good indicator that the class has an invariant)

clang-tidy: cppcoreguidelines-special-member-functions

  • (Simple) Every constructor should initialize every member variable (either explicitly, via a delegating ctor call or via default construction).
  • (Unknown) If a constructor has an Ensures contract, try to see if it holds as a postcondition.

clang-tidy: bugprone-copy-constructor-init
clang: -Wuninitialized

  • Flag classes that are copyable by = or comparable with == without a default constructor

no enforcement

  • Flag throwing default constructors

no enforcement

  • (Simple) A default constructor should do more than just initialize member variables with constants.

no enforcement but clang-tidy: cppcoreguidelines-pro-type-member-init

  • (Simple) Single-argument constructors should be declared explicit. Good single argument non-explicit constructors are rare in most code based. Warn for all that are not on a "positive list".

clang-tidy: google-explicit-constructor

  • (Simple) A member initializer list should mention the members in the same order they are declared

warning for misordered exists

  • (Simple) Every constructor should initialize every member variable (either explicitly, via a delegating ctor call or via default construction).
  • (Simple) Default arguments to constructors suggest an in-class initializer may be more appropriate.

clang-tidy: cppcoreguidelines-pro-type-member-init

  • Missing, but should be done!

no enforcement

  • (Moderate) Look for similar constructor bodies.

clang-tidy: misc-undelegated-constructor

  • Make sure that every member of the derived class is initialized.

clang-tidy: cppcoreguidelines-pro-type-member-init

  • (Simple) An assignment operator should not be virtual. Here be dragons!
  • (Simple) An assignment operator should return T& to enable chaining, not alternatives like const T& which interfere with composability and putting objects in containers.
  • (Moderate) An assignment operator should (implicitly or explicitly) invoke all base and member assignment operators. Look at the destructor to determine if the type has pointer semantics or value semantics.

no enforcement

  • (Simple) Assignment operators should not contain the pattern if (this == &a) return *this; ???

no enforcement

  • (Simple) An assignment operator should not be virtual. Here be dragons!
  • (Simple) An assignment operator should return T& to enable chaining, not alternatives like const T& which interfere with composability and putting objects in containers.
  • (Moderate) A move assignment operator should (implicitly or explicitly) invoke all base and member move assignment operators.

no enforcement

  • (Simple) A move operation should be marked noexcept.

clang-tidy: performance-noexcept-move-constructor

  • A class with any virtual function should not have a copy constructor or copy assignment operator (compiler-generated or handwritten).

no enforcement

  • (Moderate) The body of a special operation should not have the same accessibility and semantics as the compiler-generated version, because that would be redundant

clang-tidy: modernize-use-equals-default

  • The elimination of a default operation is (should be) based on the desired semantics of the class. Consider such classes suspect, but maintain a "positive list" of classes where a human has asserted that the semantics is correct.

clang-tidy: modernize-use-equals-delete

  • Flag calls of virtual functions from constructors and destructors.

**clang-static-analyzer: alpha.cplusplus.VirtualCall **

  • (Simple) A class without virtual functions should have a swap member function declared.
  • (Simple) When a class has a swap member function, it should be declared noexcept.

no enforcement

  • (Simple) When a class has a swap member function, it should be declared noexcept.

no enforcement

  • (Simple) When a class has a swap member function, it should be declared noexcept.

no enforcement, same as C.84

  • Flag an operator==() for which the argument types differ; same for other comparison operators: !=, <, <=, >, and >=.
  • Flag member operator==()s; same for other comparison operators: !=, <, <=, >, and >=.

no enforcement

  • Flag a virtual operator==(); same for other comparison operators: !=, <, <=, >, and >=.

no enforcement

  • Flag throwing hashes.

no enforcement

  • Look for classes with lots of members that do nothing but throw.
  • Flag every use of a nonpublic base class B where the derived class D does not override a virtual function or access a protected member in B, and B is not one of the following: empty, a template parameter or parameter pack of D, a class template specialized with D.

no enforcement

  • Warn on any class that contains data members and also has an overridable (non-final) virtual function.

no enforcement

  • Flag abstract classes with constructors.

no enforcement

  • A class with any virtual functions should have a destructor that is either public and virtual or else protected and nonvirtual.
  • Flag delete of a class with a virtual function but no virtual destructor.

no enforcement

  • Compare names in base and derived classes and flag uses of the same name that does not override.
  • Flag overrides with neither override nor final.
  • Flag function declarations that use more than one of virtual, override, and final.

clang-tidy: modernize-use-override,bugprone-virtual-near-miss

core-check: C26434 DONT_HIDE_METHODS

  • Flag a derived to base conversion to a base with both data and virtual functions (except for calls from a derived class member to a base class member)

no enforcement

  • Flag a class with a virtual function and a non-user-defined copy operation.
  • Flag an assignment of base class objects (objects of a class from which another has been derived).

no enforcement

  • Flag multiple get and set member functions that simply access a member without additional semantics.

no enforcement

  • Flag a class with virtual functions but no derived classes.
  • Flag a class where all member functions are virtual and have implementations.

no enforcement

  • Flag classes with protected data.

no enforcement

  • Flag any class that has non-const data members with different access levels.

no enforcement

  • Flag mixed interface and implementation hierarchies.

no enforcement

  • Diagnose name hiding

no enforcement

  • Flag uses of final.

no enforcement

  • Flag default arguments on virtual functions if they differ between base and derived declarations

no enforcement

  • Flag all slicing.

clang-tidy: cppcoreguidelines-slicing

  • Flag all uses of static_cast for downcasts, including C-style casts that perform a static_cast.

clang-tidy: cppcoreguidelines-pro-type-static-cast-downcast

  • (Complex) Unless there is a null test on the result of a dynamic_cast of a pointer type, warn upon dereference of the pointer.

no enforcement

  • Flag initialization of a naked pointer with the result of a new
  • Flag delete of local variable

clang-tidy: cppcoreguidelines-owning-memory

  • Flag the repetitive usage of template specialization list
  • Flag variables declared to be unique_ptr

clang-tidy: modernize-make-unique

  • Flag the repetitive usage of template specialization list
  • Flag variables declared to be shared_ptr

clang-tidy: modernize-make-shared

  • Flag all combinations of array decay and base to derived conversions.
  • Pass an array as a span rather than as a pointer, and don't let the array name suffer a derived-to-base conversion before getting into the span

no enforcement

  • Flag member operator functions.

no enforcement

  • Flag all conversion operators.

no enforcement

  • Tricky. Warn if & is user-defined without also defining -> for the result type.

no enforcement

  • Flag operator definitions that are not it the namespace of their operands

no enforcement

  • Tricky. Requires semantic insight.
  • Comment by Jonas: Return type must be equivalent to the builtin version (see HIC++)

no enforcement