chore(clang-tidy): Add clang-tidy rules: prefer-member-initializer and optin.performance.Padding (#3716)

* Add clang-tidy prefer-member-initializer

* Fix clang-tdy config

* Fix incorrect change

* Fix sorting of .clang-tidy
This commit is contained in:
Aaron Gokaslan 2022-02-10 12:45:46 -05:00 committed by GitHub
parent dc9803cef2
commit d6c66d25bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 47 additions and 21 deletions

View File

@ -2,10 +2,12 @@ FormatStyle: file
Checks: '
*bugprone*,
clang-analyzer-optin.performance.Padding,
clang-analyzer-optin.cplusplus.VirtualCall,
cppcoreguidelines-init-variables,
cppcoreguidelines-prefer-member-initializer,
cppcoreguidelines-pro-type-static-cast-downcast,
cppcoreguidelines-slicing,
clang-analyzer-optin.cplusplus.VirtualCall,
google-explicit-constructor,
llvm-namespace-comment,
misc-misplaced-const,

View File

@ -89,7 +89,9 @@ struct buffer_info {
? std::vector<ssize_t>(view->strides, view->strides + view->ndim)
: detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize),
(view->readonly != 0)) {
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
this->m_view = view;
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
this->ownview = ownview;
}

View File

@ -139,6 +139,7 @@ public:
// `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
// initialization race could occur as multiple threads try `gil_scoped_acquire`.
auto &internals = detail::get_internals();
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
tstate = PyEval_SaveThread();
if (disassoc) {
// Python >= 3.7 can remove this, it's an int before 3.7

View File

@ -17,12 +17,14 @@ TEST_SUBMODULE(buffers, m) {
public:
Matrix(py::ssize_t rows, py::ssize_t cols) : m_rows(rows), m_cols(cols) {
print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
m_data = new float[(size_t) (rows * cols)];
memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
}
Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
m_data = new float[(size_t) (m_rows * m_cols)];
memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
}

View File

@ -66,7 +66,11 @@ public:
std::swap(value, m.value);
return *this;
}
MoveOrCopyInt(const MoveOrCopyInt &c) { print_copy_created(this, c.value); value = c.value; }
MoveOrCopyInt(const MoveOrCopyInt &c) {
print_copy_created(this, c.value);
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
value = c.value;
}
MoveOrCopyInt &operator=(const MoveOrCopyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
~MoveOrCopyInt() { print_destroyed(this); }
@ -76,7 +80,11 @@ class CopyOnlyInt {
public:
CopyOnlyInt() { print_default_created(this); }
explicit CopyOnlyInt(int v) : value{v} { print_created(this, value); }
CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; }
CopyOnlyInt(const CopyOnlyInt &c) {
print_copy_created(this, c.value);
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
value = c.value;
}
CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
~CopyOnlyInt() { print_destroyed(this); }

View File

@ -38,8 +38,7 @@ class TestFactory2 {
explicit TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); }
public:
TestFactory2(TestFactory2 &&m) noexcept {
value = std::move(m.value);
TestFactory2(TestFactory2 &&m) noexcept : value{std::move(m.value)} {
print_move_created(this);
}
TestFactory2 &operator=(TestFactory2 &&m) noexcept {
@ -59,8 +58,7 @@ protected:
public:
explicit TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); }
TestFactory3(TestFactory3 &&m) noexcept {
value = std::move(m.value);
TestFactory3(TestFactory3 &&m) noexcept : value{std::move(m.value)} {
print_move_created(this);
}
TestFactory3 &operator=(TestFactory3 &&m) noexcept {
@ -93,10 +91,18 @@ public:
explicit TestFactory6(int i) : value{i} { print_created(this, i); }
TestFactory6(TestFactory6 &&f) noexcept {
print_move_created(this);
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
value = f.value;
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
alias = f.alias;
}
TestFactory6(const TestFactory6 &f) {
print_copy_created(this);
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
value = f.value;
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
alias = f.alias;
}
TestFactory6(const TestFactory6 &f) { print_copy_created(this); value = f.value; alias = f.alias; }
virtual ~TestFactory6() { print_destroyed(this); }
virtual int get() { return value; }
bool has_alias() const { return alias; }
@ -131,10 +137,18 @@ public:
explicit TestFactory7(int i) : value{i} { print_created(this, i); }
TestFactory7(TestFactory7 &&f) noexcept {
print_move_created(this);
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
value = f.value;
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
alias = f.alias;
}
TestFactory7(const TestFactory7 &f) {
print_copy_created(this);
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
value = f.value;
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
alias = f.alias;
}
TestFactory7(const TestFactory7 &f) { print_copy_created(this); value = f.value; alias = f.alias; }
virtual ~TestFactory7() { print_destroyed(this); }
virtual int get() { return value; }
bool has_alias() const { return alias; }
@ -142,6 +156,7 @@ public:
class PyTF7 : public TestFactory7 {
public:
explicit PyTF7(int i) : TestFactory7(i) {
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
alias = true;
print_created(this, i);
}

View File

@ -152,16 +152,19 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
public:
explicit Sequence(size_t size) : m_size(size) {
print_created(this, "of size", m_size);
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
m_data = new float[size];
memset(m_data, 0, sizeof(float) * size);
}
explicit Sequence(const std::vector<float> &value) : m_size(value.size()) {
print_created(this, "of size", m_size, "from std::vector");
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
m_data = new float[m_size];
memcpy(m_data, &value[0], sizeof(float) * m_size);
}
Sequence(const Sequence &s) : m_size(s.m_size) {
print_copy_created(this);
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
m_data = new float[m_size];
memcpy(m_data, s.m_data, sizeof(float)*m_size);
}

View File

@ -131,8 +131,7 @@ class MyObject4a;
std::unordered_set<MyObject4a *> myobject4a_instances;
class MyObject4a {
public:
explicit MyObject4a(int i) {
value = i;
explicit MyObject4a(int i) : value{i} {
print_created(this);
myobject4a_instances.insert(this);
};

View File

@ -103,10 +103,7 @@ public:
class NonCopyable {
public:
NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); }
NonCopyable(NonCopyable &&o) noexcept {
value = std::move(o.value);
print_move_created(this);
}
NonCopyable(NonCopyable &&o) noexcept : value{std::move(o.value)} { print_move_created(this); }
NonCopyable(const NonCopyable &) = delete;
NonCopyable() = delete;
void operator=(const NonCopyable &) = delete;
@ -128,11 +125,8 @@ private:
class Movable {
public:
Movable(int a, int b) : value{a+b} { print_created(this, a, b); }
Movable(const Movable &m) { value = m.value; print_copy_created(this); }
Movable(Movable &&m) noexcept {
value = m.value;
print_move_created(this);
}
Movable(const Movable &m) : value{m.value} { print_copy_created(this); }
Movable(Movable &&m) noexcept : value{m.value} { print_move_created(this); }
std::string get_value() const { return std::to_string(value); }
~Movable() { print_destroyed(this); }
private: