From ce88e940ce9db950534e7d8a411d89cb76c39f5f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 10 Sep 2020 23:20:47 -0400 Subject: [PATCH] style: clang-tidy: modernize-use-auto --- .clang-tidy | 1 + include/pybind11/cast.h | 8 ++++---- include/pybind11/numpy.h | 4 ++-- include/pybind11/pybind11.h | 12 ++++++------ include/pybind11/pytypes.h | 2 +- include/pybind11/stl_bind.h | 2 +- tests/test_numpy_array.cpp | 4 ++-- tests/test_opaque_types.cpp | 2 +- tests/test_sequences_and_iterators.cpp | 2 +- 9 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 75519bb9b..d04beea3e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -7,6 +7,7 @@ modernize-use-override, readability-container-size-empty, modernize-use-using, modernize-use-equals-default, +modernize-use-auto, ' HeaderFilterRegex: 'pybind11/.*h' diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 5ee12b82b..e3573e90d 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -432,7 +432,7 @@ PYBIND11_NOINLINE inline std::string error_string() { #if !defined(PYPY_VERSION) if (scope.trace) { - PyTracebackObject *trace = (PyTracebackObject *) scope.trace; + auto *trace = (PyTracebackObject *) scope.trace; /* Get the deepest trace possible */ while (trace->tb_next) @@ -1244,7 +1244,7 @@ template struct string_caster { load_src.ptr(), UTF_N == 8 ? "utf-8" : UTF_N == 16 ? "utf-16" : "utf-32", nullptr)); if (!utfNbytes) { PyErr_Clear(); return false; } - const CharT *buffer = reinterpret_cast(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr())); + const auto *buffer = reinterpret_cast(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr())); size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT); if (UTF_N > 8) { buffer++; length--; } // Skip BOM for UTF-16/32 value = StringType(buffer, length); @@ -1258,7 +1258,7 @@ template struct string_caster { static handle cast(const StringType &src, return_value_policy /* policy */, handle /* parent */) { const char *buffer = reinterpret_cast(src.data()); - ssize_t nbytes = ssize_t(src.size() * sizeof(CharT)); + auto nbytes = ssize_t(src.size() * sizeof(CharT)); handle s = decode_utfN(buffer, nbytes); if (!s) throw error_already_set(); return s; @@ -1364,7 +1364,7 @@ public: // errors. We also allow want to allow unicode characters U+0080 through U+00FF, as those // can fit into a single char value. if (StringCaster::UTF_N == 8 && str_len > 1 && str_len <= 4) { - unsigned char v0 = static_cast(value[0]); + auto v0 = static_cast(value[0]); size_t char0_bytes = !(v0 & 0x80) ? 1 : // low bits only: 0-127 (v0 & 0xE0) == 0xC0 ? 2 : // 0b110xxxxx - start of 2-byte sequence (v0 & 0xF0) == 0xE0 ? 3 : // 0b1110xxxx - start of 3-byte sequence diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 7c3ac121b..4c99aca88 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -1296,7 +1296,7 @@ public: m_strides.back() = static_cast(strides.back()); for (size_type i = m_strides.size() - 1; i != 0; --i) { size_type j = i - 1; - value_type s = static_cast(shape[i]); + auto s = static_cast(shape[i]); m_strides[j] = strides[j] + m_strides[i] - strides[i] * s; } } @@ -1539,7 +1539,7 @@ private: ssize_t nd = 0; std::vector shape(0); auto trivial = broadcast(buffers, nd, shape); - size_t ndim = (size_t) nd; + auto ndim = (size_t) nd; size_t size = std::accumulate(shape.begin(), shape.end(), (size_t) 1, std::multiplies()); diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 5fce339f2..ec7fc4a56 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -165,7 +165,7 @@ protected: /* Get a pointer to the capture object */ auto data = (sizeof(capture) <= sizeof(call.func.data) ? &call.func.data : call.func.data[0]); - capture *cap = const_cast(reinterpret_cast(data)); + auto *cap = const_cast(reinterpret_cast(data)); /* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */ return_value_policy policy = return_value_policy_override::policy(call.func.policy); @@ -420,7 +420,7 @@ protected: } /* Install docstring */ - PyCFunctionObject *func = (PyCFunctionObject *) m_ptr; + auto *func = (PyCFunctionObject *) m_ptr; if (func->m_ml->ml_doc) std::free(const_cast(func->m_ml->ml_doc)); func->m_ml->ml_doc = strdup(signatures.c_str()); @@ -465,7 +465,7 @@ protected: *it = overloads; /* Need to know how many arguments + keyword arguments there are to pick the right overload */ - const size_t n_args_in = (size_t) PyTuple_GET_SIZE(args_in); + const auto n_args_in = (size_t) PyTuple_GET_SIZE(args_in); handle parent = n_args_in > 0 ? PyTuple_GET_ITEM(args_in, 0) : nullptr, result = PYBIND11_TRY_NEXT_OVERLOAD; @@ -860,7 +860,7 @@ public: explicit module(const char *name, const char *doc = nullptr) { if (!options::show_user_defined_docstrings()) doc = nullptr; #if PY_MAJOR_VERSION >= 3 - PyModuleDef *def = new PyModuleDef(); + auto *def = new PyModuleDef(); std::memset(def, 0, sizeof(PyModuleDef)); def->m_name = name; def->m_doc = doc; @@ -1020,7 +1020,7 @@ protected: void install_buffer_funcs( buffer_info *(*get_buffer)(PyObject *, void *), void *get_buffer_data) { - PyHeapTypeObject *type = (PyHeapTypeObject*) m_ptr; + auto *type = (PyHeapTypeObject*) m_ptr; auto tinfo = detail::get_type_info(&type->ht_type); if (!type->ht_type.tp_as_buffer) @@ -1242,7 +1242,7 @@ public: template class_& def_buffer(Func &&func) { struct capture { Func func; }; - capture *ptr = new capture { std::forward(func) }; + auto *ptr = new capture { std::forward(func) }; install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* { detail::make_caster caster; if (!caster.load(obj, false)) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 3164764c7..9563e22f1 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1351,7 +1351,7 @@ public: buffer_info request(bool writable = false) const { int flags = PyBUF_STRIDES | PyBUF_FORMAT; if (writable) flags |= PyBUF_WRITABLE; - Py_buffer *view = new Py_buffer(); + auto *view = new Py_buffer(); if (PyObject_GetBuffer(m_ptr, view, flags) != 0) { delete view; throw error_already_set(); diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 47368f028..9d8ed0c82 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -223,7 +223,7 @@ void vector_modifiers(enable_if_treserve((size_t) slicelength); for (size_t i=0; i()) .def("numpy_view", [](py::object &obj) { py::print("ArrayClass::numpy_view()"); - ArrayClass &a = obj.cast(); + auto &a = obj.cast(); return py::array_t({2}, {4}, a.data, obj); } ); @@ -362,7 +362,7 @@ TEST_SUBMODULE(numpy_array, sm) { // test_array_resize // reshape array to 2D without changing size sm.def("array_reshape2", [](py::array_t a) { - const ssize_t dim_sz = (ssize_t)std::sqrt(a.size()); + const auto dim_sz = (ssize_t)std::sqrt(a.size()); if (dim_sz * dim_sz != a.size()) throw std::domain_error("array_reshape2: input array total size is not a squared integer"); a.resize({dim_sz, dim_sz}); diff --git a/tests/test_opaque_types.cpp b/tests/test_opaque_types.cpp index 0d20d9a01..594c45a08 100644 --- a/tests/test_opaque_types.cpp +++ b/tests/test_opaque_types.cpp @@ -60,7 +60,7 @@ TEST_SUBMODULE(opaque_types, m) { m.def("get_null_str_value", [](char *ptr) { return reinterpret_cast(ptr); }); m.def("return_unique_ptr", []() -> std::unique_ptr { - StringList *result = new StringList(); + auto *result = new StringList(); result->push_back("some value"); return std::unique_ptr(result); }); diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp index 1ce045109..545dc45d0 100644 --- a/tests/test_sequences_and_iterators.cpp +++ b/tests/test_sequences_and_iterators.cpp @@ -200,7 +200,7 @@ TEST_SUBMODULE(sequences_and_iterators, m) { size_t start, stop, step, slicelength; if (!slice.compute(s.size(), &start, &stop, &step, &slicelength)) throw py::error_already_set(); - Sequence *seq = new Sequence(slicelength); + auto *seq = new Sequence(slicelength); for (size_t i = 0; i < slicelength; ++i) { (*seq)[i] = s[start]; start += step; }