Skip to content

Commit 98da5c4

Browse files
committed
style: clang-tidy: google-readability-casting (partial)
1 parent 73b8b43 commit 98da5c4

24 files changed

+139
-136
lines changed

.clang-tidy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
FormatStyle: file
22

3+
# Partially implemented
4+
# google-readability-casting,
5+
36
Checks: '
47
llvm-namespace-comment,
58
modernize-use-override,

include/pybind11/attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ struct type_record {
289289
(base_info->default_holder ? "does not" : "does"));
290290
}
291291

292-
bases.append((PyObject *) base_info->type);
292+
bases.append(reinterpret_cast<PyObject *>(base_info->type));
293293

294294
if (base_info->type->tp_dictoffset != 0)
295295
dynamic_attr = true;

include/pybind11/buffer_info.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ struct buffer_info {
3030
detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in, bool readonly=false)
3131
: ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
3232
shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) {
33-
if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size())
33+
if (ndim != static_cast<ssize_t>(shape.size()) || ndim != static_cast<ssize_t>(strides.size()))
3434
pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length");
35-
for (size_t i = 0; i < (size_t) ndim; ++i)
35+
for (size_t i = 0; i < static_cast<size_t>(ndim); ++i)
3636
size *= shape[i];
3737
}
3838

@@ -106,7 +106,7 @@ template <typename T, typename SFINAE = void> struct compare_buffer_info {
106106

107107
template <typename T> struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
108108
static bool compare(const buffer_info& b) {
109-
return (size_t) b.itemsize == sizeof(T) && (b.format == format_descriptor<T>::value ||
109+
return static_cast<size_t>(b.itemsize) == sizeof(T) && (b.format == format_descriptor<T>::value ||
110110
((sizeof(T) == sizeof(long)) && b.format == (std::is_unsigned<T>::value ? "L" : "l")) ||
111111
((sizeof(T) == sizeof(size_t)) && b.format == (std::is_unsigned<T>::value ? "N" : "n")));
112112
}

include/pybind11/cast.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ inline std::pair<decltype(internals::registered_types_py)::iterator, bool> all_t
9595
PYBIND11_NOINLINE inline void all_type_info_populate(PyTypeObject *t, std::vector<type_info *> &bases) {
9696
std::vector<PyTypeObject *> check;
9797
for (handle parent : reinterpret_borrow<tuple>(t->tp_bases))
98-
check.push_back((PyTypeObject *) parent.ptr());
98+
check.push_back(reinterpret_cast<PyTypeObject *>(parent.ptr()));
9999

100100
auto const &type_dict = get_internals().registered_types_py;
101101
for (size_t i = 0; i < check.size(); i++) {
@@ -132,7 +132,7 @@ PYBIND11_NOINLINE inline void all_type_info_populate(PyTypeObject *t, std::vecto
132132
i--;
133133
}
134134
for (handle parent : reinterpret_borrow<tuple>(type->tp_bases))
135-
check.push_back((PyTypeObject *) parent.ptr());
135+
check.push_back(reinterpret_cast<PyTypeObject *>(parent.ptr()));
136136
}
137137
}
138138
}
@@ -204,7 +204,7 @@ PYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_index
204204

205205
PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
206206
detail::type_info *type_info = get_type_info(tp, throw_if_missing);
207-
return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
207+
return handle(type_info ? (reinterpret_cast<PyObject *>(type_info->type)) : nullptr);
208208
}
209209

210210
struct value_and_holder {
@@ -245,7 +245,7 @@ struct value_and_holder {
245245
else if (v)
246246
inst->nonsimple.status[index] |= instance::status_holder_constructed;
247247
else
248-
inst->nonsimple.status[index] &= (uint8_t) ~instance::status_holder_constructed;
248+
inst->nonsimple.status[index] &= static_cast<uint8_t>(~instance::status_holder_constructed);
249249
}
250250
bool instance_registered() const {
251251
return inst->simple_layout
@@ -258,7 +258,7 @@ struct value_and_holder {
258258
else if (v)
259259
inst->nonsimple.status[index] |= instance::status_instance_registered;
260260
else
261-
inst->nonsimple.status[index] &= (uint8_t) ~instance::status_instance_registered;
261+
inst->nonsimple.status[index] &= static_cast<uint8_t>(~instance::status_instance_registered);
262262
}
263263
};
264264

@@ -383,7 +383,7 @@ PYBIND11_NOINLINE inline void instance::allocate_layout() {
383383
// like the one we're doing here; in earlier versions (and for larger allocations) they are
384384
// just wrappers around malloc.
385385
#if PY_VERSION_HEX >= 0x03050000
386-
nonsimple.values_and_holders = (void **) PyMem_Calloc(space, sizeof(void *));
386+
nonsimple.values_and_holders = static_cast<void **>(PyMem_Calloc(space, sizeof(void *)));
387387
if (!nonsimple.values_and_holders) throw std::bad_alloc();
388388
#else
389389
nonsimple.values_and_holders = (void **) PyMem_New(void *, space);
@@ -421,7 +421,7 @@ PYBIND11_NOINLINE inline std::string error_string() {
421421
errorString += ": ";
422422
}
423423
if (scope.value)
424-
errorString += (std::string) str(scope.value);
424+
errorString += std::string(str(scope.value));
425425

426426
PyErr_NormalizeException(&scope.type, &scope.value, &scope.trace);
427427

@@ -432,7 +432,7 @@ PYBIND11_NOINLINE inline std::string error_string() {
432432

433433
#if !defined(PYPY_VERSION)
434434
if (scope.trace) {
435-
auto *trace = (PyTracebackObject *) scope.trace;
435+
auto *trace = reinterpret_cast<PyTracebackObject *>(scope.trace);
436436

437437
/* Get the deepest trace possible */
438438
while (trace->tb_next)
@@ -460,7 +460,7 @@ PYBIND11_NOINLINE inline handle get_object_handle(const void *ptr, const detail:
460460
for (auto it = range.first; it != range.second; ++it) {
461461
for (const auto &vh : values_and_holders(it->second)) {
462462
if (vh.type == type)
463-
return handle((PyObject *) it->second);
463+
return handle(reinterpret_cast<PyObject *>(it->second));
464464
}
465465
}
466466
return handle();
@@ -512,7 +512,7 @@ class type_caster_generic {
512512
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
513513
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
514514
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype))
515-
return handle((PyObject *) it_i->second).inc_ref();
515+
return handle(reinterpret_cast<PyObject *>(it_i->second)).inc_ref();
516516
}
517517
}
518518

@@ -1130,7 +1130,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
11301130
}
11311131

11321132
/* Check if this is a C++ type */
1133-
auto &bases = all_type_info((PyTypeObject *) h.get_type().ptr());
1133+
auto &bases = all_type_info(reinterpret_cast<PyTypeObject *>(h.get_type().ptr()));
11341134
if (bases.size() == 1) { // Only allowing loading from a single-value type
11351135
value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
11361136
return true;
@@ -1184,7 +1184,7 @@ template <> class type_caster<bool> {
11841184
}
11851185
#endif
11861186
if (res == 0 || res == 1) {
1187-
value = (bool) res;
1187+
value = static_cast<bool>(res);
11881188
return true;
11891189
} else {
11901190
PyErr_Clear();
@@ -1245,7 +1245,7 @@ template <typename StringType, bool IsView = false> struct string_caster {
12451245
if (!utfNbytes) { PyErr_Clear(); return false; }
12461246

12471247
const auto *buffer = reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
1248-
size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT);
1248+
size_t length = static_cast<size_t>(PYBIND11_BYTES_SIZE(utfNbytes.ptr())) / sizeof(CharT);
12491249
if (UTF_N > 8) { buffer++; length--; } // Skip BOM for UTF-16/32
12501250
value = StringType(buffer, length);
12511251

include/pybind11/detail/class.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ inline PyTypeObject *make_static_property_type() {
5353
issue no Python C API calls which could potentially invoke the
5454
garbage collector (the GC will call type_traverse(), which will in
5555
turn find the newly constructed type in an invalid state) */
56-
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
56+
auto heap_type = reinterpret_cast<PyHeapTypeObject *>(PyType_Type.tp_alloc(&PyType_Type, 0));
5757
if (!heap_type)
5858
pybind11_fail("make_static_property_type(): error allocating type!");
5959

@@ -72,7 +72,7 @@ inline PyTypeObject *make_static_property_type() {
7272
if (PyType_Ready(type) < 0)
7373
pybind11_fail("make_static_property_type(): failure in PyType_Ready()!");
7474

75-
setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
75+
setattr(reinterpret_cast<PyObject *>(type), "__module__", str("pybind11_builtins"));
7676
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
7777

7878
return type;
@@ -192,7 +192,7 @@ inline PyTypeObject* make_default_metaclass() {
192192
issue no Python C API calls which could potentially invoke the
193193
garbage collector (the GC will call type_traverse(), which will in
194194
turn find the newly constructed type in an invalid state) */
195-
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
195+
auto heap_type = reinterpret_cast<PyHeapTypeObject *>(PyType_Type.tp_alloc(&PyType_Type, 0));
196196
if (!heap_type)
197197
pybind11_fail("make_default_metaclass(): error allocating metaclass!");
198198

@@ -216,7 +216,7 @@ inline PyTypeObject* make_default_metaclass() {
216216
if (PyType_Ready(type) < 0)
217217
pybind11_fail("make_default_metaclass(): failure in PyType_Ready()!");
218218

219-
setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
219+
setattr(reinterpret_cast<PyObject *>(type), "__module__", str("pybind11_builtins"));
220220
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
221221

222222
return type;
@@ -228,7 +228,7 @@ inline PyTypeObject* make_default_metaclass() {
228228
inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self,
229229
bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
230230
for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
231-
if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
231+
if (auto parent_tinfo = get_type_info(reinterpret_cast<PyTypeObject *>(h.ptr()))) {
232232
for (auto &c : parent_tinfo->implicit_casts) {
233233
if (c.first == tinfo->cpptype) {
234234
auto *parentptr = c.second(valueptr);
@@ -403,7 +403,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
403403
issue no Python C API calls which could potentially invoke the
404404
garbage collector (the GC will call type_traverse(), which will in
405405
turn find the newly constructed type in an invalid state) */
406-
auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
406+
auto heap_type = reinterpret_cast<PyHeapTypeObject *>(metaclass->tp_alloc(metaclass, 0));
407407
if (!heap_type)
408408
pybind11_fail("make_object_base_type(): error allocating type!");
409409

@@ -428,11 +428,11 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
428428
if (PyType_Ready(type) < 0)
429429
pybind11_fail("PyType_Ready failed in make_object_base_type():" + error_string());
430430

431-
setattr((PyObject *) type, "__module__", str("pybind11_builtins"));
431+
setattr(reinterpret_cast<PyObject *>(type), "__module__", str("pybind11_builtins"));
432432
PYBIND11_SET_OLDPY_QUALNAME(type, name_obj);
433433

434434
assert(!PyType_HasFeature(type, Py_TPFLAGS_HAVE_GC));
435-
return (PyObject *) heap_type;
435+
return reinterpret_cast<PyObject *>(heap_type);
436436
}
437437

438438
/// dynamic_attr: Support for `d = instance.__dict__`.
@@ -482,7 +482,7 @@ inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
482482
#endif
483483
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
484484
type->tp_dictoffset = type->tp_basicsize; // place dict at the end
485-
type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it
485+
type->tp_basicsize += static_cast<ssize_t>(sizeof(PyObject *)); // and allocate enough space for it
486486
type->tp_traverse = pybind11_traverse;
487487
type->tp_clear = pybind11_clear;
488488

@@ -586,7 +586,7 @@ inline PyObject* make_new_python_type(const type_record &rec) {
586586
/* Allocate memory for docstring (using PyObject_MALLOC, since
587587
Python will free this later on) */
588588
size_t size = strlen(rec.doc) + 1;
589-
tp_doc = (char *) PyObject_MALLOC(size);
589+
tp_doc = static_cast<char *>(PyObject_MALLOC(size));
590590
memcpy((void *) tp_doc, rec.doc, size);
591591
}
592592

@@ -599,10 +599,10 @@ inline PyObject* make_new_python_type(const type_record &rec) {
599599
issue no Python C API calls which could potentially invoke the
600600
garbage collector (the GC will call type_traverse(), which will in
601601
turn find the newly constructed type in an invalid state) */
602-
auto metaclass = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr()
602+
auto metaclass = rec.metaclass.ptr() ? reinterpret_cast<PyTypeObject *>(rec.metaclass.ptr())
603603
: internals.default_metaclass;
604604

605-
auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
605+
auto heap_type = reinterpret_cast<PyHeapTypeObject *>(metaclass->tp_alloc(metaclass, 0));
606606
if (!heap_type)
607607
pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
608608

@@ -614,7 +614,7 @@ inline PyObject* make_new_python_type(const type_record &rec) {
614614
auto type = &heap_type->ht_type;
615615
type->tp_name = full_name;
616616
type->tp_doc = tp_doc;
617-
type->tp_base = type_incref((PyTypeObject *)base);
617+
type->tp_base = type_incref(reinterpret_cast<PyTypeObject *>(base));
618618
type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
619619
if (!bases.empty())
620620
type->tp_bases = bases.release().ptr();
@@ -652,16 +652,16 @@ inline PyObject* make_new_python_type(const type_record &rec) {
652652

653653
/* Register type with the parent scope */
654654
if (rec.scope)
655-
setattr(rec.scope, rec.name, (PyObject *) type);
655+
setattr(rec.scope, rec.name, reinterpret_cast<PyObject *>(type));
656656
else
657657
Py_INCREF(type); // Keep it alive forever (reference leak)
658658

659659
if (module) // Needed by pydoc
660-
setattr((PyObject *) type, "__module__", module);
660+
setattr(reinterpret_cast<PyObject *>(type), "__module__", module);
661661

662662
PYBIND11_SET_OLDPY_QUALNAME(type, qualname);
663663

664-
return (PyObject *) type;
664+
return reinterpret_cast<PyObject *>(type);
665665
}
666666

667667
PYBIND11_NAMESPACE_END(detail)

include/pybind11/detail/init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ template <typename Class, typename T, typename O,
293293
enable_if_t<std::is_convertible<O, handle>::value, int> = 0>
294294
void setstate(value_and_holder &v_h, std::pair<T, O> &&result, bool need_alias) {
295295
construct<Class>(v_h, std::move(result.first), need_alias);
296-
setattr((PyObject *) v_h.inst, "__dict__", result.second);
296+
setattr(reinterpret_cast<PyObject *>(v_h.inst), "__dict__", result.second);
297297
}
298298

299299
/// Implementation for py::pickle(GetState, SetState)

include/pybind11/eval.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object eval(str expr, object global = globals(), object local = object()) {
3333

3434
/* PyRun_String does not accept a PyObject / encoding specifier,
3535
this seems to be the only alternative */
36-
std::string buffer = "# -*- coding: utf-8 -*-\n" + (std::string) expr;
36+
std::string buffer = "# -*- coding: utf-8 -*-\n" + std::string(expr);
3737

3838
int start;
3939
switch (mode) {
@@ -94,7 +94,7 @@ object eval_file(str fname, object global = globals(), object local = object())
9494
}
9595

9696
int closeFile = 1;
97-
std::string fname_str = (std::string) fname;
97+
std::string fname_str = std::string(fname);
9898
#if PY_VERSION_HEX >= 0x03040000
9999
FILE *f = _Py_fopen_obj(fname.ptr(), "r");
100100
#elif PY_VERSION_HEX >= 0x03000000

0 commit comments

Comments
 (0)