Skip to content

Commit 287527f

Browse files
committed
Add readability-qualified-auto to clang-tidy
1 parent 53056b1 commit 287527f

26 files changed

+94
-93
lines changed

.clang-tidy

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

include/pybind11/attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ struct type_record {
326326
bool is_final : 1;
327327

328328
PYBIND11_NOINLINE void add_base(const std::type_info &base, void *(*caster)(void *) ) {
329-
auto base_info = detail::get_type_info(base, false);
329+
auto *base_info = detail::get_type_info(base, false);
330330
if (!base_info) {
331331
std::string tname(base.name());
332332
detail::clean_type_id(tname);

include/pybind11/cast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ class type_caster<void> : public type_caster<void_type> {
268268
}
269269

270270
/* Check if this is a C++ type */
271-
auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
271+
const const auto &bases = all_type_info((PyTypeObject *) type::handle_of(h).ptr());
272272
if (bases.size() == 1) { // Only allowing loading from a single-value type
273273
value = values_and_holders(reinterpret_cast<instance *>(h.ptr())).begin()->value_ptr();
274274
return true;
@@ -325,7 +325,7 @@ class type_caster<bool> {
325325
#else
326326
// Alternate approach for CPython: this does the same as the above, but optimized
327327
// using the CPython API so as to avoid an unneeded attribute lookup.
328-
else if (auto tp_as_number = src.ptr()->ob_type->tp_as_number) {
328+
else if (auto *tp_as_number = src.ptr()->ob_type->tp_as_number) {
329329
if (PYBIND11_NB_BOOL(tp_as_number)) {
330330
res = (*PYBIND11_NB_BOOL(tp_as_number))(src.ptr());
331331
}

include/pybind11/detail/class.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ inline PyTypeObject *make_static_property_type() {
6666
issue no Python C API calls which could potentially invoke the
6767
garbage collector (the GC will call type_traverse(), which will in
6868
turn find the newly constructed type in an invalid state) */
69-
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
69+
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
7070
if (!heap_type)
7171
pybind11_fail("make_static_property_type(): error allocating type!");
7272

@@ -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;
@@ -131,7 +131,7 @@ extern "C" inline int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyOb
131131
// 1. `Type.static_prop = value` --> descr_set: `Type.static_prop.__set__(value)`
132132
// 2. `Type.static_prop = other_static_prop` --> setattro: replace existing `static_prop`
133133
// 3. `Type.regular_attribute = value` --> setattro: regular attribute assignment
134-
const auto static_prop = (PyObject *) get_internals().static_property_type;
134+
auto *const static_prop = (PyObject *) get_internals().static_property_type;
135135
const auto call_descr_set = (descr != nullptr) && (value != nullptr)
136136
&& (PyObject_IsInstance(descr, static_prop) != 0)
137137
&& (PyObject_IsInstance(value, static_prop) == 0);
@@ -180,7 +180,7 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
180180
}
181181

182182
// This must be a pybind11 instance
183-
auto instance = reinterpret_cast<detail::instance *>(self);
183+
auto *instance = reinterpret_cast<detail::instance *>(self);
184184

185185
// Ensure that the base __init__ function(s) were called
186186
for (const auto &vh : values_and_holders(instance)) {
@@ -244,7 +244,7 @@ inline PyTypeObject *make_default_metaclass() {
244244
issue no Python C API calls which could potentially invoke the
245245
garbage collector (the GC will call type_traverse(), which will in
246246
turn find the newly constructed type in an invalid state) */
247-
auto heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
247+
auto *heap_type = (PyHeapTypeObject *) PyType_Type.tp_alloc(&PyType_Type, 0);
248248
if (!heap_type)
249249
pybind11_fail("make_default_metaclass(): error allocating metaclass!");
250250

@@ -253,7 +253,7 @@ inline PyTypeObject *make_default_metaclass() {
253253
heap_type->ht_qualname = name_obj.inc_ref().ptr();
254254
#endif
255255

256-
auto type = &heap_type->ht_type;
256+
auto *type = &heap_type->ht_type;
257257
type->tp_name = name;
258258
type->tp_base = type_incref(&PyType_Type);
259259
type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
@@ -285,7 +285,7 @@ inline void traverse_offset_bases(void *valueptr,
285285
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);
@@ -341,7 +341,7 @@ inline PyObject *make_new_instance(PyTypeObject *type) {
341341
}
342342
#endif
343343
PyObject *self = type->tp_alloc(type, 0);
344-
auto inst = reinterpret_cast<instance *>(self);
344+
auto *inst = reinterpret_cast<instance *>(self);
345345
// Allocate the value/holder internals:
346346
inst->allocate_layout();
347347

@@ -366,14 +366,14 @@ extern "C" inline int pybind11_object_init(PyObject *self, PyObject *, PyObject
366366

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

375375
inline void clear_patients(PyObject *self) {
376-
auto instance = reinterpret_cast<detail::instance *>(self);
376+
auto *instance = reinterpret_cast<detail::instance *>(self);
377377
auto &internals = get_internals();
378378
auto pos = internals.patients.find(self);
379379
assert(pos != internals.patients.end());
@@ -390,7 +390,7 @@ inline void clear_patients(PyObject *self) {
390390
/// Clears all internal data from the instance and removes it from registered instances in
391391
/// preparation for deallocation.
392392
inline void clear_instance(PyObject *self) {
393-
auto instance = reinterpret_cast<detail::instance *>(self);
393+
auto *instance = reinterpret_cast<detail::instance *>(self);
394394

395395
// Deallocate any values/holders, if present:
396396
for (auto &v_h : values_and_holders(instance)) {
@@ -426,7 +426,7 @@ inline void clear_instance(PyObject *self) {
426426
extern "C" inline void pybind11_object_dealloc(PyObject *self) {
427427
clear_instance(self);
428428

429-
auto type = Py_TYPE(self);
429+
auto *type = Py_TYPE(self);
430430
type->tp_free(self);
431431

432432
#if PY_VERSION_HEX < 0x03080000
@@ -455,7 +455,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
455455
issue no Python C API calls which could potentially invoke the
456456
garbage collector (the GC will call type_traverse(), which will in
457457
turn find the newly constructed type in an invalid state) */
458-
auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
458+
auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
459459
if (!heap_type)
460460
pybind11_fail("make_object_base_type(): error allocating type!");
461461

@@ -464,7 +464,7 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
464464
heap_type->ht_qualname = name_obj.inc_ref().ptr();
465465
#endif
466466

467-
auto type = &heap_type->ht_type;
467+
auto *type = &heap_type->ht_type;
468468
type->tp_name = name;
469469
type->tp_base = type_incref(&PyBaseObject_Type);
470470
type->tp_basicsize = static_cast<ssize_t>(sizeof(instance));
@@ -527,7 +527,7 @@ extern "C" inline int pybind11_clear(PyObject *self) {
527527

528528
/// Give instances of this type a `__dict__` and opt into garbage collection.
529529
inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
530-
auto type = &heap_type->ht_type;
530+
auto *type = &heap_type->ht_type;
531531
type->tp_flags |= Py_TPFLAGS_HAVE_GC;
532532
type->tp_dictoffset = type->tp_basicsize; // place dict at the end
533533
type->tp_basicsize += (ssize_t) sizeof(PyObject *); // and allocate enough space for it
@@ -622,7 +622,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
622622
module_ = rec.scope.attr("__name__");
623623
}
624624

625-
auto full_name = c_str(
625+
const auto *full_name = c_str(
626626
#if !defined(PYPY_VERSION)
627627
module_ ? str(module_).cast<std::string>() + "." + rec.name :
628628
#endif
@@ -639,16 +639,16 @@ inline PyObject *make_new_python_type(const type_record &rec) {
639639

640640
auto &internals = get_internals();
641641
auto bases = tuple(rec.bases);
642-
auto base = (bases.empty()) ? internals.instance_base : bases[0].ptr();
642+
auto *base = (bases.empty()) ? internals.instance_base : bases[0].ptr();
643643

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

651-
auto heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
651+
auto *heap_type = (PyHeapTypeObject *) metaclass->tp_alloc(metaclass, 0);
652652
if (!heap_type)
653653
pybind11_fail(std::string(rec.name) + ": Unable to create type object!");
654654

@@ -657,7 +657,7 @@ inline PyObject *make_new_python_type(const type_record &rec) {
657657
heap_type->ht_qualname = qualname.inc_ref().ptr();
658658
#endif
659659

660-
auto type = &heap_type->ht_type;
660+
auto *type = &heap_type->ht_type;
661661
type->tp_name = full_name;
662662
type->tp_doc = tp_doc;
663663
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
@@ -309,7 +309,7 @@ bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
309309
template <class T,
310310
enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0>
311311
bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
312-
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
312+
if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) {
313313
return handle_nested_exception(*nep, p);
314314
}
315315
return false;
@@ -346,7 +346,7 @@ inline void translate_exception(std::exception_ptr p) {
346346
return;
347347
} catch (const builtin_exception &e) {
348348
// Could not use template since it's an abstract class.
349-
if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
349+
if (const auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) {
350350
handle_nested_exception(*nep, p);
351351
}
352352
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;
@@ -176,7 +176,7 @@ inline const std::vector<detail::type_info *> &all_type_info(PyTypeObject *type)
176176
* `all_type_info` instead if you want to support multiple bases.
177177
*/
178178
PYBIND11_NOINLINE detail::type_info *get_type_info(PyTypeObject *type) {
179-
auto &bases = all_type_info(type);
179+
const auto &bases = all_type_info(type);
180180
if (bases.empty())
181181
return nullptr;
182182
if (bases.size() > 1)
@@ -205,9 +205,9 @@ inline detail::type_info *get_global_type_info(const std::type_index &tp) {
205205
/// nullptr.
206206
PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
207207
bool throw_if_missing = false) {
208-
if (auto ltype = get_local_type_info(tp))
208+
if (auto *ltype = get_local_type_info(tp))
209209
return ltype;
210-
if (auto gtype = get_global_type_info(tp))
210+
if (auto *gtype = get_global_type_info(tp))
211211
return gtype;
212212

213213
if (throw_if_missing) {
@@ -229,7 +229,7 @@ PYBIND11_NOINLINE handle find_registered_python_instance(void *src,
229229
const detail::type_info *tinfo) {
230230
auto it_instances = get_internals().registered_instances.equal_range(src);
231231
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
232-
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
232+
for (auto *instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
233233
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype))
234234
return handle((PyObject *) it_i->second).inc_ref();
235235
}
@@ -387,7 +387,7 @@ instance::get_value_and_holder(const type_info *find_type /*= nullptr default in
387387
}
388388

389389
PYBIND11_NOINLINE void instance::allocate_layout() {
390-
auto &tinfo = all_type_info(Py_TYPE(this));
390+
const auto &tinfo = all_type_info(Py_TYPE(this));
391391

392392
const size_t n_types = tinfo.size();
393393

@@ -409,7 +409,7 @@ PYBIND11_NOINLINE void instance::allocate_layout() {
409409
// values that tracks whether each associated holder has been initialized. Each [block] is
410410
// padded, if necessary, to an integer multiple of sizeof(void *).
411411
size_t space = 0;
412-
for (auto t : tinfo) {
412+
for (auto *t : tinfo) {
413413
space += 1; // value pointer
414414
space += t->holder_size_in_ptrs; // holder instance
415415
}
@@ -562,7 +562,7 @@ class type_caster_generic {
562562
return registered_inst;
563563

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

@@ -636,7 +636,7 @@ class type_caster_generic {
636636
auto *&vptr = v_h.value_ptr();
637637
// Lazy allocation for unallocated values:
638638
if (vptr == nullptr) {
639-
auto *type = v_h.type ? v_h.type : typeinfo;
639+
const auto *type = v_h.type ? v_h.type : typeinfo;
640640
if (type->operator_new) {
641641
vptr = type->operator_new(type->type_size);
642642
} else {
@@ -651,7 +651,7 @@ class type_caster_generic {
651651
value = vptr;
652652
}
653653
bool try_implicit_casts(handle src, bool convert) {
654-
for (auto &cast : typeinfo->implicit_casts) {
654+
for (const auto &cast : typeinfo->implicit_casts) {
655655
type_caster_generic sub_caster(*cast.first);
656656
if (sub_caster.load(src, convert)) {
657657
value = cast.second(sub_caster.value);
@@ -691,7 +691,7 @@ class type_caster_generic {
691691
|| (cpptype && !same_type(*cpptype, *foreign_typeinfo->cpptype)))
692692
return false;
693693

694-
if (auto result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
694+
if (auto *result = foreign_typeinfo->module_local_load(src.ptr(), foreign_typeinfo)) {
695695
value = result;
696696
return true;
697697
}
@@ -721,7 +721,7 @@ class type_caster_generic {
721721
}
722722
// Case 2: We have a derived class
723723
if (PyType_IsSubtype(srctype, typeinfo->type)) {
724-
auto &bases = all_type_info(srctype);
724+
const auto &bases = all_type_info(srctype);
725725
bool no_cpp_mi = typeinfo->simple_type;
726726

727727
// Case 2a: the python type is a Python-inherited derived class that inherits from just
@@ -738,7 +738,7 @@ class type_caster_generic {
738738
// if we can find an exact match (or, for a simple C++ type, an inherited match); if
739739
// so, we can safely reinterpret_cast to the relevant pointer.
740740
if (bases.size() > 1) {
741-
for (auto base : bases) {
741+
for (auto *base : bases) {
742742
if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type)
743743
: base->type == typeinfo->type) {
744744
this_.load_value(
@@ -757,7 +757,7 @@ class type_caster_generic {
757757

758758
// Perform an implicit conversion
759759
if (convert) {
760-
for (auto &converter : typeinfo->implicit_conversions) {
760+
for (const auto &converter : typeinfo->implicit_conversions) {
761761
auto temp = reinterpret_steal<object>(converter(src.ptr(), typeinfo->type));
762762
if (load_impl<ThisT>(temp, false)) {
763763
loader_life_support::add_patient(temp);
@@ -770,7 +770,7 @@ class type_caster_generic {
770770

771771
// Failed to match local typeinfo. Try again with global.
772772
if (typeinfo->module_local) {
773-
if (auto gtype = get_global_type_info(*typeinfo->cpptype)) {
773+
if (auto *gtype = get_global_type_info(*typeinfo->cpptype)) {
774774
typeinfo = gtype;
775775
return load(src, false);
776776
}
@@ -947,7 +947,7 @@ class type_caster_base : public type_caster_generic {
947947
// polymorphic type (using RTTI by default, but can be overridden by specializing
948948
// polymorphic_type_hook). If the instance isn't derived, returns the base version.
949949
static std::pair<const void *, const type_info *> src_and_type(const itype *src) {
950-
auto &cast_type = typeid(itype);
950+
const auto &cast_type = typeid(itype);
951951
const std::type_info *instance_type = nullptr;
952952
const void *vsrc = polymorphic_type_hook<itype>::get(src, instance_type);
953953
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
@@ -150,7 +150,7 @@ inline void set_interpreter_argv(int argc, const char *const *argv, bool add_pro
150150
widened_argv[ii] = widened_argv_entries.back().get();
151151
}
152152

153-
auto pysys_argv = widened_argv.get();
153+
auto *pysys_argv = widened_argv.get();
154154
#else
155155
// python 2.x
156156
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
@@ -44,10 +44,10 @@ struct type_caster<std::function<Return(Args...)>> {
4444
captured variables), in which case the roundtrip can be avoided.
4545
*/
4646
if (auto cfunc = func.cpp_function()) {
47-
auto cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
47+
auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
4848
if (isinstance<capsule>(cfunc_self)) {
4949
auto c = reinterpret_borrow<capsule>(cfunc_self);
50-
auto rec = (function_record *) c;
50+
auto *rec = (function_record *) c;
5151

5252
while (rec != nullptr) {
5353
if (rec->is_stateless

0 commit comments

Comments
 (0)