Skip to content

Commit 3ebbecb

Browse files
authored
Add more readability tidy rules (#5924)
* Apply clang-tidy readibility fixes Signed-off-by: Yuanyuan Chen <[email protected]> * Add checks Signed-off-by: Yuanyuan Chen <[email protected]> * More fixes Signed-off-by: cyy <[email protected]> --------- Signed-off-by: Yuanyuan Chen <[email protected]> Signed-off-by: cyy <[email protected]>
1 parent 6651530 commit 3ebbecb

16 files changed

+36
-37
lines changed

.clang-tidy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ Checks: |
4848
readability-misplaced-array-index,
4949
readability-non-const-parameter,
5050
readability-qualified-auto,
51+
readability-redundant-casting,
5152
readability-redundant-function-ptr-dereference,
53+
readability-redundant-inline-specifier,
54+
readability-redundant-member-init,
5255
readability-redundant-smartptr-get,
5356
readability-redundant-string-cstr,
5457
readability-simplify-subscript-expr,

include/pybind11/cast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ class type_caster<bool> {
475475

476476
private:
477477
// Test if an object is a NumPy boolean (without fetching the type).
478-
static inline bool is_numpy_bool(handle object) {
478+
static bool is_numpy_bool(handle object) {
479479
const char *type_name = Py_TYPE(object.ptr())->tp_name;
480480
// Name changed to `numpy.bool` in NumPy 2, `numpy.bool_` is needed for 1.x support
481481
return std::strcmp("numpy.bool", type_name) == 0

include/pybind11/detail/common.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,14 +590,10 @@ enum class return_value_policy : uint8_t {
590590

591591
PYBIND11_NAMESPACE_BEGIN(detail)
592592

593-
inline static constexpr int log2(size_t n, int k = 0) {
594-
return (n <= 1) ? k : log2(n >> 1, k + 1);
595-
}
593+
static constexpr int log2(size_t n, int k = 0) { return (n <= 1) ? k : log2(n >> 1, k + 1); }
596594

597595
// Returns the size as a multiple of sizeof(void *), rounded up.
598-
inline static constexpr size_t size_in_ptrs(size_t s) {
599-
return 1 + ((s - 1) >> log2(sizeof(void *)));
600-
}
596+
static constexpr size_t size_in_ptrs(size_t s) { return 1 + ((s - 1) >> log2(sizeof(void *))); }
601597

602598
/**
603599
* The space to allocate for simple layout instance holders (see below) in multiple of the size of

include/pybind11/detail/internals.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ template <typename value_type>
186186
using type_map = std::unordered_map<std::type_index, value_type, type_hash, type_equal_to>;
187187

188188
struct override_hash {
189-
inline size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
189+
size_t operator()(const std::pair<const PyObject *, const char *> &v) const {
190190
size_t value = std::hash<const void *>()(v.first);
191191
value ^= std::hash<const void *>()(v.second) + 0x9e3779b9 + (value << 6) + (value >> 2);
192192
return value;
@@ -555,7 +555,7 @@ class internals_pp_manager {
555555
public:
556556
using on_fetch_function = void(InternalsType *);
557557

558-
inline static internals_pp_manager &get_instance(char const *id, on_fetch_function *on_fetch) {
558+
static internals_pp_manager &get_instance(char const *id, on_fetch_function *on_fetch) {
559559
static internals_pp_manager instance(id, on_fetch);
560560
return instance;
561561
}

include/pybind11/gil_safe_call_once.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class gil_safe_call_once_and_store {
8787

8888
private:
8989
alignas(T) char storage_[sizeof(T)] = {};
90-
std::once_flag once_flag_ = {};
90+
std::once_flag once_flag_;
9191
#ifdef Py_GIL_DISABLED
9292
std::atomic_bool
9393
#else

include/pybind11/numpy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ class common_iterator {
18601860
using value_type = container_type::value_type;
18611861
using size_type = container_type::size_type;
18621862

1863-
common_iterator() : m_strides() {}
1863+
common_iterator() = default;
18641864

18651865
common_iterator(void *ptr, const container_type &strides, const container_type &shape)
18661866
: p_ptr(reinterpret_cast<char *>(ptr)), m_strides(strides.size()) {

include/pybind11/pybind11.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,9 @@ class cpp_function : public function {
811811
// so they cannot be freed. Once the function has been created, they can.
812812
// Check `make_function_record` for more details.
813813
if (free_strings) {
814-
std::free((char *) rec->name);
815-
std::free((char *) rec->doc);
816-
std::free((char *) rec->signature);
814+
std::free(rec->name);
815+
std::free(rec->doc);
816+
std::free(rec->signature);
817817
for (auto &arg : rec->args) {
818818
std::free(const_cast<char *>(arg.name));
819819
std::free(const_cast<char *>(arg.descr));
@@ -2486,8 +2486,7 @@ class class_ : public detail::generic_type {
24862486
static void init_holder_from_existing(const detail::value_and_holder &v_h,
24872487
const holder_type *holder_ptr,
24882488
std::true_type /*is_copy_constructible*/) {
2489-
new (std::addressof(v_h.holder<holder_type>()))
2490-
holder_type(*reinterpret_cast<const holder_type *>(holder_ptr));
2489+
new (std::addressof(v_h.holder<holder_type>())) holder_type(*holder_ptr);
24912490
}
24922491

24932492
static void init_holder_from_existing(const detail::value_and_holder &v_h,

include/pybind11/pytypes.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ bool isinstance(handle obj) {
861861
}
862862

863863
template <>
864-
inline bool isinstance<handle>(handle) = delete;
864+
bool isinstance<handle>(handle) = delete;
865865
template <>
866866
inline bool isinstance<object>(handle obj) {
867867
return obj.ptr() != nullptr;
@@ -994,7 +994,7 @@ inline PyObject *dict_getitem(PyObject *v, PyObject *key) {
994994

995995
inline PyObject *dict_getitemstringref(PyObject *v, const char *key) {
996996
#if PY_VERSION_HEX >= 0x030D0000
997-
PyObject *rv;
997+
PyObject *rv = nullptr;
998998
if (PyDict_GetItemStringRef(v, key, &rv) < 0) {
999999
throw error_already_set();
10001000
}
@@ -1555,7 +1555,7 @@ class iterator : public object {
15551555
}
15561556

15571557
private:
1558-
object value = {};
1558+
object value;
15591559
};
15601560

15611561
class type : public object {
@@ -1914,15 +1914,15 @@ class float_ : public object {
19141914
}
19151915
}
19161916
// NOLINTNEXTLINE(google-explicit-constructor)
1917-
float_(double value = .0) : object(PyFloat_FromDouble((double) value), stolen_t{}) {
1917+
float_(double value = .0) : object(PyFloat_FromDouble(value), stolen_t{}) {
19181918
if (!m_ptr) {
19191919
pybind11_fail("Could not allocate float object!");
19201920
}
19211921
}
19221922
// NOLINTNEXTLINE(google-explicit-constructor)
19231923
operator float() const { return (float) PyFloat_AsDouble(m_ptr); }
19241924
// NOLINTNEXTLINE(google-explicit-constructor)
1925-
operator double() const { return (double) PyFloat_AsDouble(m_ptr); }
1925+
operator double() const { return PyFloat_AsDouble(m_ptr); }
19261926
};
19271927

19281928
class weakref : public object {

include/pybind11/stl_bind.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void vector_modifiers(
244244
}
245245

246246
auto *seq = new Vector();
247-
seq->reserve((size_t) slicelength);
247+
seq->reserve(slicelength);
248248

249249
for (size_t i = 0; i < slicelength; ++i) {
250250
seq->push_back(v[start]);

tests/test_builtin_casters.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ TEST_SUBMODULE(builtin_casters, m) {
367367
m.def("complex_noconvert", [](std::complex<float> x) { return x; }, py::arg{}.noconvert());
368368

369369
// test int vs. long (Python 2)
370-
m.def("int_cast", []() { return (int) 42; });
370+
m.def("int_cast", []() { return 42; });
371371
m.def("long_cast", []() { return (long) 42; });
372372
m.def("longlong_cast", []() { return ULLONG_MAX; });
373373

0 commit comments

Comments
 (0)