Skip to content

Commit 032e73d

Browse files
Replace C-style casts to static_cast and reinterpret_cast (#5930)
* Replace C-style casts to static_cast and reinterpret_cast Signed-off-by: cyy <cyyever@outlook.com> * style: pre-commit fixes --------- Signed-off-by: cyy <cyyever@outlook.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 228f563 commit 032e73d

File tree

13 files changed

+95
-82
lines changed

13 files changed

+95
-82
lines changed

include/pybind11/attr.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ struct type_record {
373373
+ (base_has_unique_ptr_holder ? "does not" : "does"));
374374
}
375375

376-
bases.append((PyObject *) base_info->type);
376+
bases.append(reinterpret_cast<PyObject *>(base_info->type));
377377

378378
#ifdef PYBIND11_BACKWARD_COMPATIBILITY_TP_DICTOFFSET
379379
dynamic_attr |= base_info->type->tp_dictoffset != 0;
@@ -721,7 +721,9 @@ template <typename... Extra,
721721
size_t self = constexpr_sum(std::is_same<is_method, Extra>::value...)>
722722
constexpr bool expected_num_args(size_t nargs, bool has_args, bool has_kwargs) {
723723
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(nargs, has_args, has_kwargs);
724-
return named == 0 || (self + named + size_t(has_args) + size_t(has_kwargs)) == nargs;
724+
return named == 0
725+
|| (self + named + static_cast<size_t>(has_args) + static_cast<size_t>(has_kwargs))
726+
== nargs;
725727
}
726728

727729
PYBIND11_NAMESPACE_END(detail)

include/pybind11/buffer_info.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ struct buffer_info {
6666
bool readonly = false)
6767
: ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
6868
shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) {
69-
if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size()) {
69+
if (ndim != static_cast<ssize_t>(shape.size())
70+
|| ndim != static_cast<ssize_t>(strides.size())) {
7071
pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length");
7172
}
72-
for (size_t i = 0; i < (size_t) ndim; ++i) {
73+
for (size_t i = 0; i < static_cast<size_t>(ndim); ++i) {
7374
size *= shape[i];
7475
}
7576
}
@@ -195,7 +196,7 @@ struct compare_buffer_info {
195196
template <typename T>
196197
struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
197198
static bool compare(const buffer_info &b) {
198-
return (size_t) b.itemsize == sizeof(T)
199+
return static_cast<size_t>(b.itemsize) == sizeof(T)
199200
&& (b.format == format_descriptor<T>::value
200201
|| ((sizeof(T) == sizeof(long))
201202
&& b.format == (std::is_unsigned<T>::value ? "L" : "l"))

include/pybind11/cast.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ class type_caster<void> : public type_caster<void_type> {
394394
}
395395

396396
/* Check if this is a C++ type */
397-
const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
397+
const auto &bases
398+
= all_type_info(reinterpret_cast<PyTypeObject *>(type::handle_of(h).ptr()));
398399
if (bases.size() == 1) { // Only allowing loading from a single-value type
399400
value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
400401
return true;
@@ -541,7 +542,7 @@ struct string_caster {
541542

542543
const auto *buffer
543544
= reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
544-
size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT);
545+
size_t length = static_cast<size_t>(PYBIND11_BYTES_SIZE(utfNbytes.ptr())) / sizeof(CharT);
545546
// Skip BOM for UTF-16/32
546547
if (UTF_N > 8) {
547548
buffer++;

include/pybind11/detail/argument_vector.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ struct args_convert_vector {
231231
new (&m_repr.hvector) typename repr_type::heap_vector(count, value);
232232
} else {
233233
auto &inline_arr = m_repr.iarray;
234-
inline_arr.arr.fill(value ? std::size_t(-1) : 0);
234+
inline_arr.arr.fill(value ? static_cast<std::size_t>(-1) : 0);
235235
inline_arr.size = static_cast<decltype(inline_arr.size)>(count);
236236
}
237237
}
@@ -273,9 +273,9 @@ struct args_convert_vector {
273273
assert(wbi.word < kWords);
274274
assert(wbi.bit < kBitsPerWord);
275275
if (b) {
276-
ha.arr[wbi.word] |= (std::size_t(1) << wbi.bit);
276+
ha.arr[wbi.word] |= (static_cast<std::size_t>(1) << wbi.bit);
277277
} else {
278-
ha.arr[wbi.word] &= ~(std::size_t(1) << wbi.bit);
278+
ha.arr[wbi.word] &= ~(static_cast<std::size_t>(1) << wbi.bit);
279279
}
280280
assert(operator[](ha.size - 1) == b);
281281
}
@@ -300,7 +300,7 @@ struct args_convert_vector {
300300
const auto wbi = word_and_bit_index(idx);
301301
assert(wbi.word < kWords);
302302
assert(wbi.bit < kBitsPerWord);
303-
return m_repr.iarray.arr[wbi.word] & (std::size_t(1) << wbi.bit);
303+
return m_repr.iarray.arr[wbi.word] & (static_cast<std::size_t>(1) << wbi.bit);
304304
}
305305

306306
PYBIND11_NOINLINE void move_to_heap_vector_with_reserved_size(std::size_t reserved_size) {

include/pybind11/detail/class.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ inline PyTypeObject *make_static_property_type() {
7171
issue no Python C API calls which could potentially invoke the
7272
garbage collector (the GC will call type_traverse(), which will in
7373
turn find the newly constructed type in an invalid state) */
74-
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
74+
auto *heap_type = reinterpret_cast<PyHeapTypeObject *>(PyType_Type.tp_alloc(&PyType_Type, 0));
7575
if (!heap_type) {
7676
pybind11_fail("make_static_property_type(): error allocating type!");
7777
}
@@ -98,7 +98,7 @@ inline PyTypeObject *make_static_property_type() {
9898
pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
9999
}
100100

101-
setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
101+
setattr(reinterpret_cast<PyObject *>(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
102102
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
103103

104104
return type;
@@ -265,7 +265,7 @@ inline PyTypeObject *make_default_metaclass() {
265265
issue no Python C API calls which could potentially invoke the
266266
garbage collector (the GC will call type_traverse(), which will in
267267
turn find the newly constructed type in an invalid state) */
268-
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
268+
auto *heap_type = reinterpret_cast<PyHeapTypeObject *>(PyType_Type.tp_alloc(&PyType_Type, 0));
269269
if (!heap_type) {
270270
pybind11_fail("make_default_metaclass(): error allocating metaclass!");
271271
}
@@ -291,7 +291,7 @@ inline PyTypeObject *make_default_metaclass() {
291291
pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
292292
}
293293

294-
setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
294+
setattr(reinterpret_cast<PyObject *>(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
295295
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
296296

297297
return type;
@@ -306,7 +306,7 @@ inline void traverse_offset_bases(void *valueptr,
306306
instance *self,
307307
bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
308308
for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
309-
if (auto *parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
309+
if (auto *parent_tinfo = get_type_info(reinterpret_cast<PyTypeObject *>(h.ptr()))) {
310310
for (auto &c : parent_tinfo->implicit_casts) {
311311
if (c.first == tinfo->cpptype) {
312312
auto *parentptr = c.second(valueptr);
@@ -530,7 +530,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
530530
issue no Python C API calls which could potentially invoke the
531531
garbage collector (the GC will call type_traverse(), which will in
532532
turn find the newly constructed type in an invalid state) */
533-
auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
533+
auto *heap_type = reinterpret_cast<PyHeapTypeObject *>(metaclass->tp_alloc(metaclass, 0));
534534
if (!heap_type) {
535535
pybind11_fail("make_object_base_type(): error allocating type!");
536536
}
@@ -557,11 +557,11 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
557557
pybind11_fail("PyType_Ready failed in make_object_base_type(): " + error_string());
558558
}
559559

560-
setattr((PyObject *) type, "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
560+
setattr(reinterpret_cast<PyObject *>(type), "__module__", str(PYBIND11_DUMMY_MODULE_NAME));
561561
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
562562

563563
assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
564-
return (PyObject *) heap_type;
564+
return reinterpret_cast<PyObject *>(heap_type);
565565
}
566566

567567
/// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
@@ -746,7 +746,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
746746
/* Allocate memory for docstring (Python will free this later on) */
747747
size_t size = std::strlen(rec.doc) + 1;
748748
#if PY_VERSION_HEX >= 0x030D0000
749-
tp_doc = (char *) PyMem_MALLOC(size);
749+
tp_doc = static_cast<char *>(PyMem_MALLOC(size));
750750
#else
751751
tp_doc = (char *) PyObject_MALLOC(size);
752752
#endif
@@ -761,10 +761,10 @@ inline PyObject *make_new_python_type(const type_record &rec) {
761761
issue no Python C API calls which could potentially invoke the
762762
garbage collector (the GC will call type_traverse(), which will in
763763
turn find the newly constructed type in an invalid state) */
764-
auto *metaclass
765-
= rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass;
764+
auto *metaclass = rec.metaclass.ptr() ? reinterpret_cast<PyTypeObject *>(rec.metaclass.ptr())
765+
: internals.default_metaclass;
766766

767-
auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
767+
auto *heap_type = reinterpret_cast<PyHeapTypeObject *>(metaclass->tp_alloc(metaclass, 0));
768768
if (!heap_type) {
769769
pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
770770
}
@@ -777,7 +777,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
777777
auto *type = &heap_type->ht_type;
778778
type->tp_name = full_name;
779779
type->tp_doc = tp_doc;
780-
type->tp_base = type_incref((PyTypeObject *) base);
780+
type->tp_base = type_incref(reinterpret_cast<PyTypeObject *>(base));
781781
type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
782782
if (!bases.empty()) {
783783
type->tp_bases = bases.release().ptr();
@@ -818,18 +818,18 @@ inline PyObject *make_new_python_type(const type_record &rec) {
818818

819819
/* Register type with the parent scope */
820820
if (rec.scope) {
821-
setattr(rec.scope, rec.name, (PyObject *) type);
821+
setattr(rec.scope, rec.name, reinterpret_cast<PyObject *>(type));
822822
} else {
823823
Py_INCREF(type); // Keep it alive forever (reference leak)
824824
}
825825

826826
if (module_) { // Needed by pydoc
827-
setattr((PyObject *) type, "__module__", module_);
827+
setattr(reinterpret_cast<PyObject *>(type), "__module__", module_);
828828
}
829829

830830
PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
831831

832-
return (PyObject *) type;
832+
return reinterpret_cast<PyObject *>(type);
833833
}
834834

835835
PYBIND11_NAMESPACE_END(detail)

include/pybind11/detail/cpp_conduit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ inline bool type_is_managed_by_our_internals(PyTypeObject *type_obj) {
2121
return bool(internals.registered_types_py.find(type_obj)
2222
!= internals.registered_types_py.end());
2323
#else
24-
return bool(type_obj->tp_new == pybind11_object_new);
24+
return (type_obj->tp_new == pybind11_object_new);
2525
#endif
2626
}
2727

2828
inline bool is_instance_method_of_type(PyTypeObject *type_obj, PyObject *attr_name) {
2929
PyObject *descr = _PyType_Lookup(type_obj, attr_name);
30-
return bool((descr != nullptr) && PyInstanceMethod_Check(descr));
30+
return ((descr != nullptr) && PyInstanceMethod_Check(descr));
3131
}
3232

3333
inline object try_get_cpp_conduit_method(PyObject *obj) {

include/pybind11/detail/function_record_pyobject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ inline bool is_function_record_PyObject(PyObject *obj) {
126126

127127
inline function_record *function_record_ptr_from_PyObject(PyObject *obj) {
128128
if (is_function_record_PyObject(obj)) {
129-
return ((detail::function_record_PyObject *) obj)->cpp_func_rec;
129+
return (reinterpret_cast<detail::function_record_PyObject *>(obj))->cpp_func_rec;
130130
}
131131
return nullptr;
132132
}
@@ -137,7 +137,7 @@ inline object function_record_PyObject_New() {
137137
throw error_already_set();
138138
}
139139
py_func_rec->cpp_func_rec = nullptr; // For clarity/purity. Redundant in practice.
140-
return reinterpret_steal<object>((PyObject *) py_func_rec);
140+
return reinterpret_steal<object>(reinterpret_cast<PyObject *>(py_func_rec));
141141
}
142142

143143
PYBIND11_NAMESPACE_BEGIN(function_record_PyTypeObject_methods)

include/pybind11/detail/init.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,9 @@ void setstate(value_and_holder &v_h, std::pair<T, O> &&result, bool need_alias)
476476
return;
477477
}
478478
// Our tests never run into an unset dict, but being careful here for now (see #5658)
479-
auto dict = getattr((PyObject *) v_h.inst, "__dict__", none());
479+
auto dict = getattr(reinterpret_cast<PyObject *>(v_h.inst), "__dict__", none());
480480
if (dict.is_none()) {
481-
setattr((PyObject *) v_h.inst, "__dict__", d);
481+
setattr(reinterpret_cast<PyObject *>(v_h.inst), "__dict__", d);
482482
} else {
483483
// Keep the original object dict and just update it
484484
if (PyDict_Update(dict.ptr(), d.ptr()) < 0) {

include/pybind11/detail/internals.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,8 @@ class internals_pp_manager {
606606
// this could be called without an active interpreter, just use what was cached
607607
if (!tstate || tstate->interp == last_istate_tls()) {
608608
auto tpp = internals_p_tls();
609-
if (tpp) {
610-
delete tpp;
611-
}
609+
610+
delete tpp;
612611
}
613612
unref();
614613
return;

include/pybind11/detail/type_caster_base.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
125125
assert(bases.empty());
126126
std::vector<PyTypeObject *> check;
127127
for (handle parent : reinterpret_borrow<tuple>(t->tp_bases)) {
128-
check.push_back((PyTypeObject *) parent.ptr());
128+
check.push_back(reinterpret_cast<PyTypeObject *>(parent.ptr()));
129129
}
130130
auto const &type_dict = get_internals().registered_types_py;
131131
for (size_t i = 0; i < check.size(); i++) {
@@ -168,7 +168,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
168168
i--;
169169
}
170170
for (handle parent : reinterpret_borrow<tuple>(type->tp_bases)) {
171-
check.push_back((PyTypeObject *) parent.ptr());
171+
check.push_back(reinterpret_cast<PyTypeObject *>(parent.ptr()));
172172
}
173173
}
174174
}
@@ -286,7 +286,7 @@ PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_info &tp,
286286

287287
PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
288288
detail::type_info *type_info = get_type_info(tp, throw_if_missing);
289-
return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
289+
return handle(type_info ? (reinterpret_cast<PyObject *>(type_info->type)) : nullptr);
290290
}
291291

292292
inline bool try_incref(PyObject *obj) {
@@ -506,7 +506,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() {
506506
// efficient for small allocations like the one we're doing here;
507507
// for larger allocations they are just wrappers around malloc.
508508
// TODO: is this still true for pure Python 3.6?
509-
nonsimple.values_and_holders = (void **) PyMem_Calloc(space, sizeof(void *));
509+
nonsimple.values_and_holders = static_cast<void **>(PyMem_Calloc(space, sizeof(void *)));
510510
if (!nonsimple.values_and_holders) {
511511
throw std::bad_alloc();
512512
}
@@ -537,7 +537,7 @@ PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_i
537537
for (auto it = range.first; it != range.second; ++it) {
538538
for (const auto &vh : values_and_holders(it->second)) {
539539
if (vh.type == type) {
540-
return handle((PyObject *) it->second);
540+
return handle(reinterpret_cast<PyObject *>(it->second));
541541
}
542542
}
543543
}
@@ -1700,7 +1700,7 @@ inline std::string quote_cpp_type_name(const std::string &cpp_type_name) {
17001700

17011701
PYBIND11_NOINLINE std::string type_info_description(const std::type_info &ti) {
17021702
if (auto *type_data = get_type_info(ti)) {
1703-
handle th((PyObject *) type_data->type);
1703+
handle th(reinterpret_cast<PyObject *>(type_data->type));
17041704
return th.attr("__module__").cast<std::string>() + '.'
17051705
+ th.attr("__qualname__").cast<std::string>();
17061706
}

0 commit comments

Comments
 (0)