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)),
      |                                       ^
This commit is contained in:
Ralf W. Grosse-Kunstleve
2025-04-10 12:09:59 -07:00
committed by GitHub
parent 708ce4d9c7
commit 1bd1d1ced8

View File

@@ -32,17 +32,17 @@ void tp_free_impl(void *self);
static PyObject *reduce_ex_impl(PyObject *self, PyObject *, PyObject *);
PYBIND11_WARNING_PUSH
#if defined(__GNUC__) && __GNUC__ >= 8
PYBIND11_WARNING_DISABLE_GCC("-Wcast-function-type")
#endif
#if defined(__clang__) && !defined(__apple_build_version__) && __clang_major__ >= 19
PYBIND11_WARNING_DISABLE_CLANG("-Wcast-function-type-mismatch")
#endif
static PyMethodDef tp_methods_impl[]
= {{"__reduce_ex__", (PyCFunction) reduce_ex_impl, METH_VARARGS | METH_KEYWORDS, nullptr},
= {{"__reduce_ex__",
// reduce_ex_impl is a PyCFunctionWithKeywords, but PyMethodDef
// requires a PyCFunction. The cast through void* is safe and
// idiomatic with METH_KEYWORDS, and it successfully sidesteps
// unhelpful compiler warnings.
// NOLINTNEXTLINE(bugprone-casting-through-void)
reinterpret_cast<PyCFunction>(reinterpret_cast<void *>(reduce_ex_impl)),
METH_VARARGS | METH_KEYWORDS,
nullptr},
{nullptr, nullptr, 0, nullptr}};
PYBIND11_WARNING_POP
// Note that this name is versioned.
constexpr char tp_name_impl[]