From 1bd1d1ced8868204df95a75c3cbf0f34a5204dfa Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 10 Apr 2025 12:09:59 -0700 Subject: [PATCH] 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(reinterpret_cast(reduce_ex_impl)), | ^ --- .../pybind11/detail/function_record_pyobject.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/pybind11/detail/function_record_pyobject.h b/include/pybind11/detail/function_record_pyobject.h index fce08f3cf..4cc8b242e 100644 --- a/include/pybind11/detail/function_record_pyobject.h +++ b/include/pybind11/detail/function_record_pyobject.h @@ -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(reinterpret_cast(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[]