Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-tidy readability-qualified-auto #3702

Merged
merged 2 commits into from Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-tidy
Expand Up @@ -40,6 +40,7 @@ readability-implicit-bool-conversion,
readability-make-member-function-const,
readability-misplaced-array-index,
readability-non-const-parameter,
readability-qualified-auto,
readability-redundant-function-ptr-dereference,
readability-redundant-smartptr-get,
readability-redundant-string-cstr,
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/attr.h
Expand Up @@ -311,7 +311,7 @@ struct type_record {
bool is_final : 1;

PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
auto base_info = detail::get_type_info(base, false);
auto *base_info = detail::get_type_info(base, false);
if (!base_info) {
std::string tname(base.name());
detail::clean_type_id(tname);
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/cast.h
Expand Up @@ -251,7 +251,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
}

/* Check if this is a C++ type */
auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
if (bases.size() == 1) { // Only allowing loading from a single-value type
value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
return true;
Expand Down Expand Up @@ -306,7 +306,7 @@ template <> class type_caster<bool> {
#else
// Alternate approach for CPython: this does the same as the above, but optimized
// using the CPython API so as to avoid an unneeded attribute lookup.
else if (auto tp_as_number = src.ptr()->ob_type->tp_as_number) {
else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) {
if (PYBIND11_NB_BOOL(tp_as_number)) {
res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr());
}
Expand Down
45 changes: 22 additions & 23 deletions include/pybind11/detail/class.h
Expand Up @@ -65,7 +65,7 @@ inline PyTypeObject *make_static_property_type() {
issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
if (!heap_type) {
pybind11_fail("make_static_property_type(): error allocating type!");
}
Expand All @@ -75,7 +75,7 @@ inline PyTypeObject *make_static_property_type() {
heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif

auto type = &heap_type->ht_type;
auto *type = &heap_type->ht_type;
type->tp_name = name;
type->tp_base = type_incref(&PyProperty_Type);
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
Expand Down Expand Up @@ -130,7 +130,7 @@ extern "C" inline int pybind11_meta_setattro(PyObject* obj, PyObject* name, PyOb
// 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)`
// 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop`
// 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment
const auto static_prop = (PyObject *) get_internals().static_property_type;
auto *const static_prop = (PyObject *) get_internals().static_property_type;
const auto call_descr_set = (descr != nullptr) && (value != nullptr)
&& (PyObject_IsInstance(descr, static_prop) != 0)
&& (PyObject_IsInstance(value, static_prop) == 0);
Expand Down Expand Up @@ -179,7 +179,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
}

// This must be a pybind11 instance
auto instance = reinterpret_cast<detail::instance *>(self);
auto *instance = reinterpret_cast<detail::instance *>(self);

// Ensure that the base __init__ function(s) were called
for (const auto &vh : values_and_holders(instance)) {
Expand Down Expand Up @@ -245,7 +245,7 @@ inline PyTypeObject* make_default_metaclass() {
issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
if (!heap_type) {
pybind11_fail("make_default_metaclass(): error allocating metaclass!");
}
Expand All @@ -255,7 +255,7 @@ inline PyTypeObject* make_default_metaclass() {
heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif

auto type = &heap_type->ht_type;
auto *type = &heap_type->ht_type;
type->tp_name = name;
type->tp_base = type_incref(&PyType_Type);
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
Expand Down Expand Up @@ -285,7 +285,7 @@ inline PyTypeObject* make_default_metaclass() {
inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self,
bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
if (auto *parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
for (auto &c : parent_tinfo->implicit_casts) {
if (c.first == tinfo->cpptype) {
auto *parentptr = c.second(valueptr);
Expand Down Expand Up @@ -344,7 +344,7 @@ inline PyObject *make_new_instance(PyTypeObject *type) {
}
#endif
PyObject *self = type->tp_alloc(type, 0);
auto inst = reinterpret_cast<instance *>(self);
auto *inst = reinterpret_cast<instance *>(self);
// Allocate the value/holder internals:
inst->allocate_layout();

Expand All @@ -369,14 +369,14 @@ extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject

inline void add_patient(PyObject *nurse, PyObject *patient) {
auto &internals = get_internals();
auto instance = reinterpret_cast<detail::instance *>(nurse);
auto *instance = reinterpret_cast<detail::instance *>(nurse);
instance->has_patients = true;
Py_INCREF(patient);
internals.patients[nurse].push_back(patient);
}

inline void clear_patients(PyObject *self) {
auto instance = reinterpret_cast<detail::instance *>(self);
auto *instance = reinterpret_cast<detail::instance *>(self);
auto &internals = get_internals();
auto pos = internals.patients.find(self);
assert(pos != internals.patients.end());
Expand All @@ -394,7 +394,7 @@ inline void clear_patients(PyObject *self) {
/// Clears all internal data from the instance and removes it from registered instances in
/// preparation for deallocation.
inline void clear_instance(PyObject *self) {
auto instance = reinterpret_cast<detail::instance *>(self);
auto *instance = reinterpret_cast<detail::instance *>(self);

// Deallocate any values/holders, if present:
for (auto &v_h : values_and_holders(instance)) {
Expand Down Expand Up @@ -435,7 +435,7 @@ inline void clear_instance(PyObject *self) {
extern "C" inline void pybind11_object_dealloc(PyObject *self) {
clear_instance(self);

auto type = Py_TYPE(self);
auto *type = Py_TYPE(self);
type->tp_free(self);

#if PY_VERSION_HEX < 0x03080000
Expand Down Expand Up @@ -464,7 +464,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */
auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
if (!heap_type) {
pybind11_fail("make_object_base_type(): error allocating type!");
}
Expand All @@ -474,7 +474,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
heap_type->ht_qualname = name_obj.inc_ref().ptr();
#endif

auto type = &heap_type->ht_type;
auto *type = &heap_type->ht_type;
type->tp_name = name;
type->tp_base = type_incref(&PyBaseObject_Type);
type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
Expand Down Expand Up @@ -538,7 +538,7 @@ extern "C" inline int pybind11_clear(PyObject *self) {

/// Give instances of this type a `__dict__` and opt into garbage collection.
inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
auto type = &heap_type->ht_type;
auto *type = &heap_type->ht_type;
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
type->tp_dictoffset = type->tp_basicsize; // place dict at the end
type->tp_basicsize += (ssize_t)sizeof(PyObject *); // and allocate enough space for it
Expand Down Expand Up @@ -639,11 +639,11 @@ inline PyObject* make_new_python_type(const type_record &rec) {
}
}

auto full_name = c_str(
const auto *full_name = c_str(
#if !defined(PYPY_VERSION)
module_ ? str(module_).cast<std::string>() + "." + rec.name :
#endif
rec.name);
rec.name);

char *tp_doc = nullptr;
if (rec.doc && options::show_user_defined_docstrings()) {
Expand All @@ -656,17 +656,16 @@ inline PyObject* make_new_python_type(const type_record &rec) {

auto &internals = get_internals();
auto bases = tuple(rec.bases);
auto base = (bases.empty()) ? internals.instance_base
: bases[0].ptr();
auto *base = (bases.empty()) ? internals.instance_base : bases[0].ptr();

/* Danger zone: from now (and until PyType_Ready), make sure to
issue no Python C API calls which could potentially invoke the
garbage collector (the GC will call type_traverse(), which will in
turn find the newly constructed type in an invalid state) */
auto metaclass = rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr()
: internals.default_metaclass;
auto *metaclass
= rec.metaclass.ptr() ? (PyTypeObject *) rec.metaclass.ptr() : internals.default_metaclass;

auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
if (!heap_type) {
pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
}
Expand All @@ -676,7 +675,7 @@ inline PyObject* make_new_python_type(const type_record &rec) {
heap_type->ht_qualname = qualname.inc_ref().ptr();
#endif

auto type = &heap_type->ht_type;
auto *type = &heap_type->ht_type;
type->tp_name = full_name;
type->tp_doc = tp_doc;
type->tp_base = type_incref((PyTypeObject *)base);
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/detail/internals.h
Expand Up @@ -301,7 +301,7 @@ bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
template <class T,
enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
return handle_nested_exception(*nep, p);
}
return false;
Expand Down Expand Up @@ -338,7 +338,7 @@ inline void translate_exception(std::exception_ptr p) {
return;
} catch (const builtin_exception &e) {
// Could not use template since it's an abstract class.
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
handle_nested_exception(*nep, p);
}
e.set_error();
Expand Down
32 changes: 16 additions & 16 deletions include/pybind11/detail/type_caster_base.h
Expand Up @@ -112,7 +112,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_

auto const &type_dict = get_internals().registered_types_py;
for (size_t i = 0; i < check.size(); i++) {
auto type = check[i];
auto *type = check[i];
// Ignore Python2 old-style class super type:
if (!PyType_Check((PyObject *) type)) {
continue;
Expand Down Expand Up @@ -181,7 +181,7 @@ inline const std::vector<detail::type_info *> &all_type_info(PyTypeObject *type)
* `all_type_info` instead if you want to support multiple bases.
*/
PYBIND11_NOINLINE detail::type_info* get_type_info(PyTypeObject *type) {
auto &bases = all_type_info(type);
const auto &bases = all_type_info(type);
if (bases.empty()) {
return nullptr;
}
Expand Down Expand Up @@ -213,10 +213,10 @@ inline detail::type_info *get_global_type_info(const std::type_index &tp) {
/// Return the type info for a given C++ type; on lookup failure can either throw or return nullptr.
PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
bool throw_if_missing = false) {
if (auto ltype = get_local_type_info(tp)) {
if (auto *ltype = get_local_type_info(tp)) {
return ltype;
}
if (auto gtype = get_global_type_info(tp)) {
if (auto *gtype = get_global_type_info(tp)) {
return gtype;
}

Expand All @@ -238,7 +238,7 @@ PYBIND11_NOINLINE handle find_registered_python_instance(void *src,
const detail::type_info *tinfo) {
auto it_instances = get_internals().registered_instances.equal_range(src);
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) {
return handle((PyObject *) it_i->second).inc_ref();
}
Expand Down Expand Up @@ -400,7 +400,7 @@ PYBIND11_NOINLINE value_and_holder instance::get_value_and_holder(const type_inf
}

PYBIND11_NOINLINE void instance::allocate_layout() {
auto &tinfo = all_type_info(Py_TYPE(this));
const auto &tinfo = all_type_info(Py_TYPE(this));

const size_t n_types = tinfo.size();

Expand All @@ -424,7 +424,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() {
// values that tracks whether each associated holder has been initialized. Each [block] is
// padded, if necessary, to an integer multiple of sizeof(void *).
size_t space = 0;
for (auto t : tinfo) {
for (auto *t : tinfo) {
space += 1; // value pointer
space += t->holder_size_in_ptrs; // holder instance
}
Expand Down Expand Up @@ -585,7 +585,7 @@ class type_caster_generic {
}

auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
auto wrapper = reinterpret_cast<instance *>(inst.ptr());
auto *wrapper = reinterpret_cast<instance *>(inst.ptr());
wrapper->owned = false;
void *&valueptr = values_and_holders(wrapper).begin()->value_ptr();

Expand Down Expand Up @@ -659,7 +659,7 @@ class type_caster_generic {
auto *&vptr = v_h.value_ptr();
// Lazy allocation for unallocated values:
if (vptr == nullptr) {
auto *type = v_h.type ? v_h.type : typeinfo;
const auto *type = v_h.type ? v_h.type : typeinfo;
if (type->operator_new) {
vptr = type->operator_new(type->type_size);
} else {
Expand All @@ -678,7 +678,7 @@ class type_caster_generic {
value = vptr;
}
bool try_implicit_casts(handle src, bool convert) {
for (auto &cast : typeinfo->implicit_casts) {
for (const auto &cast : typeinfo->implicit_casts) {
type_caster_generic sub_caster(*cast.first);
if (sub_caster.load(src, convert)) {
value = cast.second(sub_caster.value);
Expand Down Expand Up @@ -721,7 +721,7 @@ class type_caster_generic {
return false;
}

if (auto result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
if (auto *result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
value = result;
return true;
}
Expand Down Expand Up @@ -753,7 +753,7 @@ class type_caster_generic {
}
// Case 2: We have a derived class
if (PyType_IsSubtype(srctype, typeinfo->type)) {
auto &bases = all_type_info(srctype);
const auto &bases = all_type_info(srctype);
bool no_cpp_mi = typeinfo->simple_type;

// Case 2a: the python type is a Python-inherited derived class that inherits from just
Expand All @@ -770,7 +770,7 @@ class type_caster_generic {
// we can find an exact match (or, for a simple C++ type, an inherited match); if so, we
// can safely reinterpret_cast to the relevant pointer.
if (bases.size() > 1) {
for (auto base : bases) {
for (auto *base : bases) {
if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) : base->type == typeinfo->type) {
this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base));
return true;
Expand All @@ -788,7 +788,7 @@ class type_caster_generic {

// Perform an implicit conversion
if (convert) {
for (auto &converter : typeinfo->implicit_conversions) {
for (const auto &converter : typeinfo->implicit_conversions) {
auto temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
if (load_impl<ThisT>(temp, false)) {
loader_life_support::add_patient(temp);
Expand All @@ -802,7 +802,7 @@ class type_caster_generic {

// Failed to match local typeinfo. Try again with global.
if (typeinfo->module_local) {
if (auto gtype = get_global_type_info(*typeinfo->cpptype)) {
if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) {
typeinfo = gtype;
return load(src, false);
}
Expand Down Expand Up @@ -973,7 +973,7 @@ template <typename type> class type_caster_base : public type_caster_generic {
// polymorphic type (using RTTI by default, but can be overridden by specializing
// polymorphic_type_hook). If the instance isn't derived, returns the base version.
static std::pair<const void *, const type_info *> src_and_type(const itype *src) {
auto &cast_type = typeid(itype);
const auto &cast_type = typeid(itype);
const std::type_info *instance_type = nullptr;
const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type);
if (instance_type && !same_type(cast_type, *instance_type)) {
Expand Down
2 changes: 1 addition & 1 deletion include/pybind11/embed.h
Expand Up @@ -157,7 +157,7 @@ inline void set_interpreter_argv(int argc, const char *const *argv, bool add_pro
widened_argv[ii] = widened_argv_entries.back().get();
}

auto pysys_argv = widened_argv.get();
auto *pysys_argv = widened_argv.get();
#else
// python 2.x
std::vector<std::string> strings{safe_argv, safe_argv + argv_size};
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/functional.h
Expand Up @@ -46,10 +46,10 @@ struct type_caster<std::function<Return(Args...)>> {
captured variables), in which case the roundtrip can be avoided.
*/
if (auto cfunc = func.cpp_function()) {
auto cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
if (isinstance<capsule>(cfunc_self)) {
auto c = reinterpret_borrow<capsule>(cfunc_self);
auto rec = (function_record *) c;
auto *rec = (function_record *) c;

while (rec != nullptr) {
if (rec->is_stateless
Expand Down