mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 22:39:09 +00:00
fix(clang-tidy): performance fixes applied in tests and CI (#3051)
* Initial fixes * Whoops * Finish clang-tidy manual fixes * Add two missing fixes * Revert * Update clang-tidy * Try to fix unreachable code error * Move nolint comment * Apply missing fix * Don't override clang-tidy config * Does this fix clang-tidy? * Make all clang-tidy errors visible * Add comments about NOLINTs and remove a few * Fix typo
This commit is contained in:
@@ -22,14 +22,18 @@ public:
|
||||
ExampleMandA(int value) : value(value) { print_created(this, value); }
|
||||
ExampleMandA(const ExampleMandA &e) : value(e.value) { print_copy_created(this); }
|
||||
ExampleMandA(std::string&&) {}
|
||||
ExampleMandA(ExampleMandA &&e) : value(e.value) { print_move_created(this); }
|
||||
ExampleMandA(ExampleMandA &&e) noexcept : value(e.value) { print_move_created(this); }
|
||||
~ExampleMandA() { print_destroyed(this); }
|
||||
|
||||
std::string toString() const { return "ExampleMandA[value=" + std::to_string(value) + "]"; }
|
||||
|
||||
void operator=(const ExampleMandA &e) { print_copy_assigned(this); value = e.value; }
|
||||
void operator=(ExampleMandA &&e) { print_move_assigned(this); value = e.value; }
|
||||
void operator=(ExampleMandA &&e) noexcept {
|
||||
print_move_assigned(this);
|
||||
value = e.value;
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(performance-unnecessary-value-param)
|
||||
void add1(ExampleMandA other) { value += other.value; } // passing by value
|
||||
void add2(ExampleMandA &other) { value += other.value; } // passing by reference
|
||||
void add3(const ExampleMandA &other) { value += other.value; } // passing by const reference
|
||||
@@ -112,7 +116,7 @@ int none1(const NoneTester &obj) { return obj.answer; }
|
||||
int none2(NoneTester *obj) { return obj ? obj->answer : -1; }
|
||||
int none3(std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
|
||||
int none4(std::shared_ptr<NoneTester> *obj) { return obj && *obj ? (*obj)->answer : -1; }
|
||||
int none5(std::shared_ptr<NoneTester> obj) { return obj ? obj->answer : -1; }
|
||||
int none5(const std::shared_ptr<NoneTester> &obj) { return obj ? obj->answer : -1; }
|
||||
|
||||
struct StrIssue {
|
||||
int val = -1;
|
||||
@@ -226,36 +230,41 @@ TEST_SUBMODULE(methods_and_attributes, m) {
|
||||
.def(py::init<>())
|
||||
.def_readonly("def_readonly", &TestProperties::value)
|
||||
.def_readwrite("def_readwrite", &TestProperties::value)
|
||||
.def_property("def_writeonly", nullptr,
|
||||
[](TestProperties& s,int v) { s.value = v; } )
|
||||
.def_property("def_writeonly", nullptr, [](TestProperties &s, int v) { s.value = v; })
|
||||
.def_property("def_property_writeonly", nullptr, &TestProperties::set)
|
||||
.def_property_readonly("def_property_readonly", &TestProperties::get)
|
||||
.def_property("def_property", &TestProperties::get, &TestProperties::set)
|
||||
.def_property("def_property_impossible", nullptr, nullptr)
|
||||
.def_readonly_static("def_readonly_static", &TestProperties::static_value)
|
||||
.def_readwrite_static("def_readwrite_static", &TestProperties::static_value)
|
||||
.def_property_static("def_writeonly_static", nullptr,
|
||||
[](py::object, int v) { TestProperties::static_value = v; })
|
||||
.def_property_readonly_static("def_property_readonly_static",
|
||||
[](py::object) { return TestProperties::static_get(); })
|
||||
.def_property_static("def_property_writeonly_static", nullptr,
|
||||
[](py::object, int v) { return TestProperties::static_set(v); })
|
||||
.def_property_static("def_property_static",
|
||||
[](py::object) { return TestProperties::static_get(); },
|
||||
[](py::object, int v) { TestProperties::static_set(v); })
|
||||
.def_property_static("static_cls",
|
||||
[](py::object cls) { return cls; },
|
||||
[](py::object cls, py::function f) { f(cls); });
|
||||
.def_property_static("def_writeonly_static",
|
||||
nullptr,
|
||||
[](const py::object &, int v) { TestProperties::static_value = v; })
|
||||
.def_property_readonly_static(
|
||||
"def_property_readonly_static",
|
||||
[](const py::object &) { return TestProperties::static_get(); })
|
||||
.def_property_static(
|
||||
"def_property_writeonly_static",
|
||||
nullptr,
|
||||
[](const py::object &, int v) { return TestProperties::static_set(v); })
|
||||
.def_property_static(
|
||||
"def_property_static",
|
||||
[](const py::object &) { return TestProperties::static_get(); },
|
||||
[](const py::object &, int v) { TestProperties::static_set(v); })
|
||||
.def_property_static(
|
||||
"static_cls",
|
||||
[](py::object cls) { return cls; },
|
||||
[](const py::object &cls, const py::function &f) { f(cls); });
|
||||
|
||||
py::class_<TestPropertiesOverride, TestProperties>(m, "TestPropertiesOverride")
|
||||
.def(py::init<>())
|
||||
.def_readonly("def_readonly", &TestPropertiesOverride::value)
|
||||
.def_readonly_static("def_readonly_static", &TestPropertiesOverride::static_value);
|
||||
|
||||
auto static_get1 = [](py::object) -> const UserType & { return TestPropRVP::sv1; };
|
||||
auto static_get2 = [](py::object) -> const UserType & { return TestPropRVP::sv2; };
|
||||
auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.set(v); };
|
||||
auto static_set2 = [](py::object, int v) { TestPropRVP::sv2.set(v); };
|
||||
auto static_get1 = [](const py::object &) -> const UserType & { return TestPropRVP::sv1; };
|
||||
auto static_get2 = [](const py::object &) -> const UserType & { return TestPropRVP::sv2; };
|
||||
auto static_set1 = [](const py::object &, int v) { TestPropRVP::sv1.set(v); };
|
||||
auto static_set2 = [](const py::object &, int v) { TestPropRVP::sv2.set(v); };
|
||||
auto rvp_copy = py::return_value_policy::copy;
|
||||
|
||||
// test_property_return_value_policies
|
||||
@@ -266,24 +275,30 @@ TEST_SUBMODULE(methods_and_attributes, m) {
|
||||
.def_property_readonly("ro_func", py::cpp_function(&TestPropRVP::get2, rvp_copy))
|
||||
.def_property("rw_ref", &TestPropRVP::get1, &TestPropRVP::set1)
|
||||
.def_property("rw_copy", &TestPropRVP::get2, &TestPropRVP::set2, rvp_copy)
|
||||
.def_property("rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2)
|
||||
.def_property(
|
||||
"rw_func", py::cpp_function(&TestPropRVP::get2, rvp_copy), &TestPropRVP::set2)
|
||||
.def_property_readonly_static("static_ro_ref", static_get1)
|
||||
.def_property_readonly_static("static_ro_copy", static_get2, rvp_copy)
|
||||
.def_property_readonly_static("static_ro_func", py::cpp_function(static_get2, rvp_copy))
|
||||
.def_property_static("static_rw_ref", static_get1, static_set1)
|
||||
.def_property_static("static_rw_copy", static_get2, static_set2, rvp_copy)
|
||||
.def_property_static("static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
|
||||
.def_property_static(
|
||||
"static_rw_func", py::cpp_function(static_get2, rvp_copy), static_set2)
|
||||
// test_property_rvalue_policy
|
||||
.def_property_readonly("rvalue", &TestPropRVP::get_rvalue)
|
||||
// NOLINTNEXTLINE(performance-unnecessary-value-param)
|
||||
.def_property_readonly_static("static_rvalue", [](py::object) { return UserType(1); });
|
||||
|
||||
// test_metaclass_override
|
||||
struct MetaclassOverride { };
|
||||
py::class_<MetaclassOverride>(m, "MetaclassOverride", py::metaclass((PyObject *) &PyType_Type))
|
||||
// NOLINTNEXTLINE(performance-unnecessary-value-param)
|
||||
.def_property_readonly_static("readonly", [](py::object) { return 1; });
|
||||
|
||||
// test_overload_ordering
|
||||
// NOLINTNEXTLINE(performance-unnecessary-value-param)
|
||||
m.def("overload_order", [](std::string) { return 1; });
|
||||
// NOLINTNEXTLINE(performance-unnecessary-value-param)
|
||||
m.def("overload_order", [](std::string) { return 2; });
|
||||
m.def("overload_order", [](int) { return 3; });
|
||||
m.def("overload_order", [](int) { return 4; }, py::prepend{});
|
||||
|
||||
Reference in New Issue
Block a user