style: clang-tidy: modernize-use-auto
diff --git a/.clang-tidy b/.clang-tidy
index 75519bb..d04beea 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -7,6 +7,7 @@
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 5ee12b8..e3573e9 100644
--- a/include/pybind11/cast.h
+++ b/include/pybind11/cast.h
@@ -432,7 +432,7 @@
#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 @@
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<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
+ const auto *buffer = reinterpret_cast<const CharT *>(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 @@
static handle cast(const StringType &src, return_value_policy /* policy */, handle /* parent */) {
const char *buffer = reinterpret_cast<const char *>(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 @@
// 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<unsigned char>(value[0]);
+ auto v0 = static_cast<unsigned char>(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 7c3ac12..4c99aca 100644
--- a/include/pybind11/numpy.h
+++ b/include/pybind11/numpy.h
@@ -1296,7 +1296,7 @@
m_strides.back() = static_cast<value_type>(strides.back());
for (size_type i = m_strides.size() - 1; i != 0; --i) {
size_type j = i - 1;
- value_type s = static_cast<value_type>(shape[i]);
+ auto s = static_cast<value_type>(shape[i]);
m_strides[j] = strides[j] + m_strides[i] - strides[i] * s;
}
}
@@ -1539,7 +1539,7 @@
ssize_t nd = 0;
std::vector<ssize_t> 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<size_t>());
diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h
index 5fce339..ec7fc4a 100644
--- a/include/pybind11/pybind11.h
+++ b/include/pybind11/pybind11.h
@@ -165,7 +165,7 @@
/* 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<capture *>(reinterpret_cast<const capture *>(data));
+ auto *cap = const_cast<capture *>(reinterpret_cast<const capture *>(data));
/* Override policy for rvalues -- usually to enforce rvp::move on an rvalue */
return_value_policy policy = return_value_policy_override<Return>::policy(call.func.policy);
@@ -420,7 +420,7 @@
}
/* Install docstring */
- PyCFunctionObject *func = (PyCFunctionObject *) m_ptr;
+ auto *func = (PyCFunctionObject *) m_ptr;
if (func->m_ml->ml_doc)
std::free(const_cast<char *>(func->m_ml->ml_doc));
func->m_ml->ml_doc = strdup(signatures.c_str());
@@ -465,7 +465,7 @@
*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 @@
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 @@
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 @@
template <typename Func> class_& def_buffer(Func &&func) {
struct capture { Func func; };
- capture *ptr = new capture { std::forward<Func>(func) };
+ auto *ptr = new capture { std::forward<Func>(func) };
install_buffer_funcs([](PyObject *obj, void *ptr) -> buffer_info* {
detail::make_caster<type> caster;
if (!caster.load(obj, false))
diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h
index 3164764..9563e22 100644
--- a/include/pybind11/pytypes.h
+++ b/include/pybind11/pytypes.h
@@ -1351,7 +1351,7 @@
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 47368f0..9d8ed0c 100644
--- a/include/pybind11/stl_bind.h
+++ b/include/pybind11/stl_bind.h
@@ -223,7 +223,7 @@
if (!slice.compute(v.size(), &start, &stop, &step, &slicelength))
throw error_already_set();
- Vector *seq = new Vector();
+ auto *seq = new Vector();
seq->reserve((size_t) slicelength);
for (size_t i=0; i<slicelength; ++i) {
diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp
index caa0525..33f1d78 100644
--- a/tests/test_numpy_array.cpp
+++ b/tests/test_numpy_array.cpp
@@ -212,7 +212,7 @@
.def(py::init<>())
.def("numpy_view", [](py::object &obj) {
py::print("ArrayClass::numpy_view()");
- ArrayClass &a = obj.cast<ArrayClass&>();
+ auto &a = obj.cast<ArrayClass&>();
return py::array_t<int>({2}, {4}, a.data, obj);
}
);
@@ -362,7 +362,7 @@
// test_array_resize
// reshape array to 2D without changing size
sm.def("array_reshape2", [](py::array_t<double> 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 0d20d9a..594c45a 100644
--- a/tests/test_opaque_types.cpp
+++ b/tests/test_opaque_types.cpp
@@ -60,7 +60,7 @@
m.def("get_null_str_value", [](char *ptr) { return reinterpret_cast<std::intptr_t>(ptr); });
m.def("return_unique_ptr", []() -> std::unique_ptr<StringList> {
- StringList *result = new StringList();
+ auto *result = new StringList();
result->push_back("some value");
return std::unique_ptr<StringList>(result);
});
diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp
index 1ce0451..545dc45 100644
--- a/tests/test_sequences_and_iterators.cpp
+++ b/tests/test_sequences_and_iterators.cpp
@@ -200,7 +200,7 @@
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;
}