Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions include/pybind11/buffer_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these suppressed instead of fixed?

this->m_view = view;
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
this->ownview = ownview;
}

Expand Down
1 change: 1 addition & 0 deletions include/pybind11/gil.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class gil_scoped_release {
// `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
Expand Down
4 changes: 3 additions & 1 deletion tests/test_buffers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
m_data = new float[(size_t) (rows*cols)];
// 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));
}
Expand Down
12 changes: 10 additions & 2 deletions tests/test_copy_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ class MoveOrCopyInt {
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); }

Expand All @@ -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); }

Expand Down
27 changes: 21 additions & 6 deletions tests/test_factory_constructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -59,8 +58,7 @@ class TestFactory3 {

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 {
Expand Down Expand Up @@ -93,10 +91,18 @@ class TestFactory6 {
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; }
Expand Down Expand Up @@ -131,17 +137,26 @@ class TestFactory7 {
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; }
};
class PyTF7 : public TestFactory7 {
public:
explicit PyTF7(int i) : TestFactory7(i) {
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
alias = true;
print_created(this, i);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sequences_and_iterators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions tests/test_smart_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
12 changes: 3 additions & 9 deletions tests/test_virtual_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ class PyExampleVirt : public ExampleVirt {
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;
Expand All @@ -128,11 +125,8 @@ class NonCopyable {
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:
Expand Down