Skip to content

Commit 1bd1d1c

Browse files
authored
Change PyCFunction cast in function_record_pyobject.h to sidestep unhelpful compiler warnings. (#5608)
* Change PyCFunction cast in function_record_pyobject.h to sidestep unhelpful compiler warnings. * Resolve clang-tidy error /__w/pybind11/pybind11/include/pybind11/detail/function_record_pyobject.h:41:39: error: do not cast 'PyObject *(PyObject *, PyObject *, PyObject *)' (aka '_object *(_object *, _object *, _object *)') to 'PyCFunction' (aka '_object *(*)(_object *, _object *)') through 'void *' [bugprone-casting-through-void,-warnings-as-errors] 41 | reinterpret_cast<PyCFunction>(reinterpret_cast<void *>(reduce_ex_impl)), | ^
1 parent 708ce4d commit 1bd1d1c

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

include/pybind11/detail/function_record_pyobject.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ void tp_free_impl(void *self);
3232

3333
static PyObject *reduce_ex_impl(PyObject *self, PyObject *, PyObject *);
3434

35-
PYBIND11_WARNING_PUSH
36-
#if defined(__GNUC__) && __GNUC__ >= 8
37-
PYBIND11_WARNING_DISABLE_GCC("-Wcast-function-type")
38-
#endif
39-
#if defined(__clang__) && !defined(__apple_build_version__) && __clang_major__ >= 19
40-
PYBIND11_WARNING_DISABLE_CLANG("-Wcast-function-type-mismatch")
41-
#endif
4235
static PyMethodDef tp_methods_impl[]
43-
= {{"__reduce_ex__", (PyCFunction) reduce_ex_impl, METH_VARARGS | METH_KEYWORDS, nullptr},
36+
= {{"__reduce_ex__",
37+
// reduce_ex_impl is a PyCFunctionWithKeywords, but PyMethodDef
38+
// requires a PyCFunction. The cast through void* is safe and
39+
// idiomatic with METH_KEYWORDS, and it successfully sidesteps
40+
// unhelpful compiler warnings.
41+
// NOLINTNEXTLINE(bugprone-casting-through-void)
42+
reinterpret_cast<PyCFunction>(reinterpret_cast<void *>(reduce_ex_impl)),
43+
METH_VARARGS | METH_KEYWORDS,
44+
nullptr},
4445
{nullptr, nullptr, 0, nullptr}};
45-
PYBIND11_WARNING_POP
4646

4747
// Note that this name is versioned.
4848
constexpr char tp_name_impl[]

0 commit comments

Comments
 (0)