Skip to content

Commit 7769e77

Browse files
rwgkhenryiii
andauthored
clang-tidy readability-qualified-auto (#3702)
* Adding readability-qualified-auto to .clang-tidy Ported from @henryiii's 287527f * fix: support Python < 3.6 Co-authored-by: Henry Schreiner <[email protected]>
1 parent b4f5350 commit 7769e77

25 files changed

+109
-106
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ readability-implicit-bool-conversion,
4040
readability-make-member-function-const,
4141
readability-misplaced-array-index,
4242
readability-non-const-parameter,
43+
readability-qualified-auto,
4344
readability-redundant-function-ptr-dereference,
4445
readability-redundant-smartptr-get,
4546
readability-redundant-string-cstr,

include/pybind11/attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ struct type_record {
311311
bool is_final : 1;
312312

313313
PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *)) {
314-
auto base_info = detail::get_type_info(base, false);
314+
auto *base_info = detail::get_type_info(base, false);
315315
if (!base_info) {
316316
std::string tname(base.name());
317317
detail::clean_type_id(tname);

include/pybind11/cast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
251251
}
252252

253253
/* Check if this is a C++ type */
254-
auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
254+
const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
255255
if (bases.size() == 1) { // Only allowing loading from a single-value type
256256
value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
257257
return true;
@@ -306,7 +306,7 @@ template <> class type_caster<bool> {
306306
#else
307307
// Alternate approach for CPython: this does the same as the above, but optimized
308308
// using the CPython API so as to avoid an unneeded attribute lookup.
309-
else if (auto tp_as_number = src.ptr()->ob_type->tp_as_number) {
309+
else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) {
310310
if (PYBIND11_NB_BOOL(tp_as_number)) {
311311
res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr());
312312
}

include/pybind11/detail/class.h

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ inline PyTypeObject *make_static_property_type() {
6565
issue no Python C API calls which could potentially invoke the
6666
garbage collector (the GC will call type_traverse(), which will in
6767
turn find the newly constructed type in an invalid state) */
68-
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
68+
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
6969
if (!heap_type) {
7070
pybind11_fail("make_static_property_type(): error allocating type!");
7171
}
@@ -75,7 +75,7 @@ inline PyTypeObject *make_static_property_type() {
7575
heap_type->ht_qualname = name_obj.inc_ref().ptr();
7676
#endif
7777

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

181181
// This must be a pybind11 instance
182-
auto instance = reinterpret_cast<detail::instance *>(self);
182+
auto *instance = reinterpret_cast<detail::instance *>(self);
183183

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

258-
auto type = &heap_type->ht_type;
258+
auto *type = &heap_type->ht_type;
259259
type->tp_name = name;
260260
type->tp_base = type_incref(&PyType_Type);
261261
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
@@ -285,7 +285,7 @@ inline PyTypeObject* make_default_metaclass() {
285285
inline void traverse_offset_bases(void *valueptr, const detail::type_info *tinfo, instance *self,
286286
bool (*f)(void * /*parentptr*/, instance * /*self*/)) {
287287
for (handle h : reinterpret_borrow<tuple>(tinfo->type->tp_bases)) {
288-
if (auto parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
288+
if (auto *parent_tinfo = get_type_info((PyTypeObject *) h.ptr())) {
289289
for (auto &c : parent_tinfo->implicit_casts) {
290290
if (c.first == tinfo->cpptype) {
291291
auto *parentptr = c.second(valueptr);
@@ -344,7 +344,7 @@ inline PyObject *make_new_instance(PyTypeObject *type) {
344344
}
345345
#endif
346346
PyObject *self = type->tp_alloc(type, 0);
347-
auto inst = reinterpret_cast<instance *>(self);
347+
auto *inst = reinterpret_cast<instance *>(self);
348348
// Allocate the value/holder internals:
349349
inst->allocate_layout();
350350

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

370370
inline void add_patient(PyObject *nurse, PyObject *patient) {
371371
auto &internals = get_internals();
372-
auto instance = reinterpret_cast<detail::instance *>(nurse);
372+
auto *instance = reinterpret_cast<detail::instance *>(nurse);
373373
instance->has_patients = true;
374374
Py_INCREF(patient);
375375
internals.patients[nurse].push_back(patient);
376376
}
377377

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

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

438-
auto type = Py_TYPE(self);
438+
auto *type = Py_TYPE(self);
439439
type->tp_free(self);
440440

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

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

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

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

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

657657
auto &internals = get_internals();
658658
auto bases = tuple(rec.bases);
659-
auto base = (bases.empty()) ? internals.instance_base
660-
: bases[0].ptr();
659+
auto *base = (bases.empty()) ? internals.instance_base : bases[0].ptr();
661660

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

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

679-
auto type = &heap_type->ht_type;
678+
auto *type = &heap_type->ht_type;
680679
type->tp_name = full_name;
681680
type->tp_doc = tp_doc;
682681
type->tp_base = type_incref((PyTypeObject *)base);

include/pybind11/detail/internals.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
301301
template <class T,
302302
enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
303303
bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
304-
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
304+
if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
305305
return handle_nested_exception(*nep, p);
306306
}
307307
return false;
@@ -338,7 +338,7 @@ inline void translate_exception(std::exception_ptr p) {
338338
return;
339339
} catch (const builtin_exception &e) {
340340
// Could not use template since it's an abstract class.
341-
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
341+
if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
342342
handle_nested_exception(*nep, p);
343343
}
344344
e.set_error();

include/pybind11/detail/type_caster_base.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
109109

110110
auto const &type_dict = get_internals().registered_types_py;
111111
for (size_t i = 0; i < check.size(); i++) {
112-
auto type = check[i];
112+
auto *type = check[i];
113113
// Ignore Python2 old-style class super type:
114114
if (!PyType_Check((PyObject *) type)) {
115115
continue;
@@ -178,7 +178,7 @@ inline const std::vector<detail::type_info *> &all_type_info(PyTypeObject *type)
178178
* `all_type_info` instead if you want to support multiple bases.
179179
*/
180180
PYBIND11_NOINLINE detail::type_info* get_type_info(PyTypeObject *type) {
181-
auto &bases = all_type_info(type);
181+
const auto &bases = all_type_info(type);
182182
if (bases.empty()) {
183183
return nullptr;
184184
}
@@ -210,10 +210,10 @@ inline detail::type_info *get_global_type_info(const std::type_index &tp) {
210210
/// Return the type info for a given C++ type; on lookup failure can either throw or return nullptr.
211211
PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
212212
bool throw_if_missing = false) {
213-
if (auto ltype = get_local_type_info(tp)) {
213+
if (auto *ltype = get_local_type_info(tp)) {
214214
return ltype;
215215
}
216-
if (auto gtype = get_global_type_info(tp)) {
216+
if (auto *gtype = get_global_type_info(tp)) {
217217
return gtype;
218218
}
219219

@@ -235,7 +235,7 @@ PYBIND11_NOINLINE handle find_registered_python_instance(void *src,
235235
const detail::type_info *tinfo) {
236236
auto it_instances = get_internals().registered_instances.equal_range(src);
237237
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
238-
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
238+
for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
239239
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype)) {
240240
return handle((PyObject *) it_i->second).inc_ref();
241241
}
@@ -397,7 +397,7 @@ PYBIND11_NOINLINE value_and_holder instance::get_value_and_holder(const type_inf
397397
}
398398

399399
PYBIND11_NOINLINE void instance::allocate_layout() {
400-
auto &tinfo = all_type_info(Py_TYPE(this));
400+
const auto &tinfo = all_type_info(Py_TYPE(this));
401401

402402
const size_t n_types = tinfo.size();
403403

@@ -421,7 +421,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() {
421421
// values that tracks whether each associated holder has been initialized. Each [block] is
422422
// padded, if necessary, to an integer multiple of sizeof(void *).
423423
size_t space = 0;
424-
for (auto t : tinfo) {
424+
for (auto *t : tinfo) {
425425
space += 1; // value pointer
426426
space += t->holder_size_in_ptrs; // holder instance
427427
}
@@ -582,7 +582,7 @@ class type_caster_generic {
582582
}
583583

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

@@ -656,7 +656,7 @@ class type_caster_generic {
656656
auto *&vptr = v_h.value_ptr();
657657
// Lazy allocation for unallocated values:
658658
if (vptr == nullptr) {
659-
auto *type = v_h.type ? v_h.type : typeinfo;
659+
const auto *type = v_h.type ? v_h.type : typeinfo;
660660
if (type->operator_new) {
661661
vptr = type->operator_new(type->type_size);
662662
} else {
@@ -675,7 +675,7 @@ class type_caster_generic {
675675
value = vptr;
676676
}
677677
bool try_implicit_casts(handle src, bool convert) {
678-
for (auto &cast : typeinfo->implicit_casts) {
678+
for (const auto &cast : typeinfo->implicit_casts) {
679679
type_caster_generic sub_caster(*cast.first);
680680
if (sub_caster.load(src, convert)) {
681681
value = cast.second(sub_caster.value);
@@ -718,7 +718,7 @@ class type_caster_generic {
718718
return false;
719719
}
720720

721-
if (auto result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
721+
if (auto *result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
722722
value = result;
723723
return true;
724724
}
@@ -750,7 +750,7 @@ class type_caster_generic {
750750
}
751751
// Case 2: We have a derived class
752752
if (PyType_IsSubtype(srctype, typeinfo->type)) {
753-
auto &bases = all_type_info(srctype);
753+
const auto &bases = all_type_info(srctype);
754754
bool no_cpp_mi = typeinfo->simple_type;
755755

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

786786
// Perform an implicit conversion
787787
if (convert) {
788-
for (auto &converter : typeinfo->implicit_conversions) {
788+
for (const auto &converter : typeinfo->implicit_conversions) {
789789
auto temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
790790
if (load_impl<ThisT>(temp, false)) {
791791
loader_life_support::add_patient(temp);
@@ -799,7 +799,7 @@ class type_caster_generic {
799799

800800
// Failed to match local typeinfo. Try again with global.
801801
if (typeinfo->module_local) {
802-
if (auto gtype = get_global_type_info(*typeinfo->cpptype)) {
802+
if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) {
803803
typeinfo = gtype;
804804
return load(src, false);
805805
}
@@ -970,7 +970,7 @@ template <typename type> class type_caster_base : public type_caster_generic {
970970
// polymorphic type (using RTTI by default, but can be overridden by specializing
971971
// polymorphic_type_hook). If the instance isn't derived, returns the base version.
972972
static std::pair<const void *, const type_info *> src_and_type(const itype *src) {
973-
auto &cast_type = typeid(itype);
973+
const auto &cast_type = typeid(itype);
974974
const std::type_info *instance_type = nullptr;
975975
const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type);
976976
if (instance_type && !same_type(cast_type, *instance_type)) {

include/pybind11/embed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ inline void set_interpreter_argv(int argc, const char *const *argv, bool add_pro
157157
widened_argv[ii] = widened_argv_entries.back().get();
158158
}
159159

160-
auto pysys_argv = widened_argv.get();
160+
auto *pysys_argv = widened_argv.get();
161161
#else
162162
// python 2.x
163163
std::vector<std::string> strings{safe_argv, safe_argv + argv_size};

include/pybind11/functional.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ struct type_caster<std::function<Return(Args...)>> {
4646
captured variables), in which case the roundtrip can be avoided.
4747
*/
4848
if (auto cfunc = func.cpp_function()) {
49-
auto cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
49+
auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
5050
if (isinstance<capsule>(cfunc_self)) {
5151
auto c = reinterpret_borrow<capsule>(cfunc_self);
52-
auto rec = (function_record *) c;
52+
auto *rec = (function_record *) c;
5353

5454
while (rec != nullptr) {
5555
if (rec->is_stateless

0 commit comments

Comments
 (0)