Skip to content

Commit d6c66d2

Browse files
authored
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
1 parent dc9803c commit d6c66d2

9 files changed

+47
-21
lines changed

.clang-tidy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ FormatStyle: file
22

33
Checks: '
44
*bugprone*,
5+
clang-analyzer-optin.performance.Padding,
6+
clang-analyzer-optin.cplusplus.VirtualCall,
57
cppcoreguidelines-init-variables,
8+
cppcoreguidelines-prefer-member-initializer,
69
cppcoreguidelines-pro-type-static-cast-downcast,
710
cppcoreguidelines-slicing,
8-
clang-analyzer-optin.cplusplus.VirtualCall,
911
google-explicit-constructor,
1012
llvm-namespace-comment,
1113
misc-misplaced-const,

include/pybind11/buffer_info.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ struct buffer_info {
8989
? std::vector<ssize_t>(view->strides, view->strides + view->ndim)
9090
: detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize),
9191
(view->readonly != 0)) {
92+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
9293
this->m_view = view;
94+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
9395
this->ownview = ownview;
9496
}
9597

include/pybind11/gil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class gil_scoped_release {
139139
// `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
140140
// initialization race could occur as multiple threads try `gil_scoped_acquire`.
141141
auto &internals = detail::get_internals();
142+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
142143
tstate = PyEval_SaveThread();
143144
if (disassoc) {
144145
// Python >= 3.7 can remove this, it's an int before 3.7

tests/test_buffers.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ TEST_SUBMODULE(buffers, m) {
1717
public:
1818
Matrix(py::ssize_t rows, py::ssize_t cols) : m_rows(rows), m_cols(cols) {
1919
print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
20-
m_data = new float[(size_t) (rows*cols)];
20+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
21+
m_data = new float[(size_t) (rows * cols)];
2122
memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
2223
}
2324

2425
Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
2526
print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
27+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
2628
m_data = new float[(size_t) (m_rows * m_cols)];
2729
memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
2830
}

tests/test_copy_move.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ class MoveOrCopyInt {
6666
std::swap(value, m.value);
6767
return *this;
6868
}
69-
MoveOrCopyInt(const MoveOrCopyInt &c) { print_copy_created(this, c.value); value = c.value; }
69+
MoveOrCopyInt(const MoveOrCopyInt &c) {
70+
print_copy_created(this, c.value);
71+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
72+
value = c.value;
73+
}
7074
MoveOrCopyInt &operator=(const MoveOrCopyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
7175
~MoveOrCopyInt() { print_destroyed(this); }
7276

@@ -76,7 +80,11 @@ class CopyOnlyInt {
7680
public:
7781
CopyOnlyInt() { print_default_created(this); }
7882
explicit CopyOnlyInt(int v) : value{v} { print_created(this, value); }
79-
CopyOnlyInt(const CopyOnlyInt &c) { print_copy_created(this, c.value); value = c.value; }
83+
CopyOnlyInt(const CopyOnlyInt &c) {
84+
print_copy_created(this, c.value);
85+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
86+
value = c.value;
87+
}
8088
CopyOnlyInt &operator=(const CopyOnlyInt &c) { print_copy_assigned(this, c.value); value = c.value; return *this; }
8189
~CopyOnlyInt() { print_destroyed(this); }
8290

tests/test_factory_constructors.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class TestFactory2 {
3838
explicit TestFactory2(std::string v) : value(std::move(v)) { print_created(this, value); }
3939

4040
public:
41-
TestFactory2(TestFactory2 &&m) noexcept {
42-
value = std::move(m.value);
41+
TestFactory2(TestFactory2 &&m) noexcept : value{std::move(m.value)} {
4342
print_move_created(this);
4443
}
4544
TestFactory2 &operator=(TestFactory2 &&m) noexcept {
@@ -59,8 +58,7 @@ class TestFactory3 {
5958

6059
public:
6160
explicit TestFactory3(std::string v) : value(std::move(v)) { print_created(this, value); }
62-
TestFactory3(TestFactory3 &&m) noexcept {
63-
value = std::move(m.value);
61+
TestFactory3(TestFactory3 &&m) noexcept : value{std::move(m.value)} {
6462
print_move_created(this);
6563
}
6664
TestFactory3 &operator=(TestFactory3 &&m) noexcept {
@@ -93,10 +91,18 @@ class TestFactory6 {
9391
explicit TestFactory6(int i) : value{i} { print_created(this, i); }
9492
TestFactory6(TestFactory6 &&f) noexcept {
9593
print_move_created(this);
94+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
95+
value = f.value;
96+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
97+
alias = f.alias;
98+
}
99+
TestFactory6(const TestFactory6 &f) {
100+
print_copy_created(this);
101+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
96102
value = f.value;
103+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
97104
alias = f.alias;
98105
}
99-
TestFactory6(const TestFactory6 &f) { print_copy_created(this); value = f.value; alias = f.alias; }
100106
virtual ~TestFactory6() { print_destroyed(this); }
101107
virtual int get() { return value; }
102108
bool has_alias() const { return alias; }
@@ -131,17 +137,26 @@ class TestFactory7 {
131137
explicit TestFactory7(int i) : value{i} { print_created(this, i); }
132138
TestFactory7(TestFactory7 &&f) noexcept {
133139
print_move_created(this);
140+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
141+
value = f.value;
142+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
143+
alias = f.alias;
144+
}
145+
TestFactory7(const TestFactory7 &f) {
146+
print_copy_created(this);
147+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
134148
value = f.value;
149+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
135150
alias = f.alias;
136151
}
137-
TestFactory7(const TestFactory7 &f) { print_copy_created(this); value = f.value; alias = f.alias; }
138152
virtual ~TestFactory7() { print_destroyed(this); }
139153
virtual int get() { return value; }
140154
bool has_alias() const { return alias; }
141155
};
142156
class PyTF7 : public TestFactory7 {
143157
public:
144158
explicit PyTF7(int i) : TestFactory7(i) {
159+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
145160
alias = true;
146161
print_created(this, i);
147162
}

tests/test_sequences_and_iterators.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,19 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
152152
public:
153153
explicit Sequence(size_t size) : m_size(size) {
154154
print_created(this, "of size", m_size);
155+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
155156
m_data = new float[size];
156157
memset(m_data, 0, sizeof(float) * size);
157158
}
158159
explicit Sequence(const std::vector<float> &value) : m_size(value.size()) {
159160
print_created(this, "of size", m_size, "from std::vector");
161+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
160162
m_data = new float[m_size];
161163
memcpy(m_data, &value[0], sizeof(float) * m_size);
162164
}
163165
Sequence(const Sequence &s) : m_size(s.m_size) {
164166
print_copy_created(this);
167+
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
165168
m_data = new float[m_size];
166169
memcpy(m_data, s.m_data, sizeof(float)*m_size);
167170
}

tests/test_smart_ptr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ class MyObject4a;
131131
std::unordered_set<MyObject4a *> myobject4a_instances;
132132
class MyObject4a {
133133
public:
134-
explicit MyObject4a(int i) {
135-
value = i;
134+
explicit MyObject4a(int i) : value{i} {
136135
print_created(this);
137136
myobject4a_instances.insert(this);
138137
};

tests/test_virtual_functions.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ class PyExampleVirt : public ExampleVirt {
103103
class NonCopyable {
104104
public:
105105
NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); }
106-
NonCopyable(NonCopyable &&o) noexcept {
107-
value = std::move(o.value);
108-
print_move_created(this);
109-
}
106+
NonCopyable(NonCopyable &&o) noexcept : value{std::move(o.value)} { print_move_created(this); }
110107
NonCopyable(const NonCopyable &) = delete;
111108
NonCopyable() = delete;
112109
void operator=(const NonCopyable &) = delete;
@@ -128,11 +125,8 @@ class NonCopyable {
128125
class Movable {
129126
public:
130127
Movable(int a, int b) : value{a+b} { print_created(this, a, b); }
131-
Movable(const Movable &m) { value = m.value; print_copy_created(this); }
132-
Movable(Movable &&m) noexcept {
133-
value = m.value;
134-
print_move_created(this);
135-
}
128+
Movable(const Movable &m) : value{m.value} { print_copy_created(this); }
129+
Movable(Movable &&m) noexcept : value{m.value} { print_move_created(this); }
136130
std::string get_value() const { return std::to_string(value); }
137131
~Movable() { print_destroyed(this); }
138132
private:

0 commit comments

Comments
 (0)