Skip to content

Commit e7d146b

Browse files
committed
Merge branch 'master' into smart_holder
2 parents c03061f + b5357d1 commit e7d146b

17 files changed

+114
-115
lines changed

.clang-tidy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
FormatStyle: file
22

33
Checks: '
4+
clang-analyzer-optin.cplusplus.VirtualCall,
45
llvm-namespace-comment,
56
misc-misplaced-const,
67
misc-static-assert,
78
misc-uniqueptr-reset-release,
89
modernize-avoid-bind,
10+
modernize-redundant-void-arg,
911
modernize-replace-auto-ptr,
1012
modernize-replace-disallow-copy-and-assign-macro,
1113
modernize-shrink-to-fit,
@@ -20,6 +22,7 @@ modernize-use-override,
2022
modernize-use-using,
2123
*performance*,
2224
readability-container-size-empty,
25+
readability-else-after-return,
2326
readability-make-member-function-const,
2427
readability-redundant-function-ptr-dereference,
2528
readability-redundant-smartptr-get,

include/pybind11/cast.h

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -107,25 +107,28 @@ template <typename type> class type_caster<std::reference_wrapper<type>> {
107107
operator std::reference_wrapper<type>() { return cast_op<type &>(subcaster); }
108108
};
109109

110-
#define PYBIND11_TYPE_CASTER(type, py_name) \
111-
protected: \
112-
type value; \
113-
public: \
114-
static constexpr auto name = py_name; \
115-
template <typename T_, enable_if_t<std::is_same<type, remove_cv_t<T_>>::value, int> = 0> \
116-
static handle cast(T_ *src, return_value_policy policy, handle parent) { \
117-
if (!src) return none().release(); \
118-
if (policy == return_value_policy::take_ownership) { \
119-
auto h = cast(std::move(*src), policy, parent); delete src; return h; \
120-
} else { \
121-
return cast(*src, policy, parent); \
122-
} \
123-
} \
124-
operator type*() { return &value; } \
125-
operator type&() { return value; } \
126-
operator type&&() && { return std::move(value); } \
127-
template <typename T_> using cast_op_type = pybind11::detail::movable_cast_op_type<T_>
128-
110+
#define PYBIND11_TYPE_CASTER(type, py_name) \
111+
protected: \
112+
type value; \
113+
\
114+
public: \
115+
static constexpr auto name = py_name; \
116+
template <typename T_, enable_if_t<std::is_same<type, remove_cv_t<T_>>::value, int> = 0> \
117+
static handle cast(T_ *src, return_value_policy policy, handle parent) { \
118+
if (!src) \
119+
return none().release(); \
120+
if (policy == return_value_policy::take_ownership) { \
121+
auto h = cast(std::move(*src), policy, parent); \
122+
delete src; \
123+
return h; \
124+
} \
125+
return cast(*src, policy, parent); \
126+
} \
127+
operator type *() { return &value; } \
128+
operator type &() { return value; } \
129+
operator type &&() && { return std::move(value); } \
130+
template <typename T_> \
131+
using cast_op_type = pybind11::detail::movable_cast_op_type<T_>
129132

130133
template <typename CharT> using is_std_char_type = any_of<
131134
std::is_same<CharT, char>, /* std::string */
@@ -269,7 +272,8 @@ template <> class type_caster<void> : public type_caster<void_type> {
269272
bool load(handle h, bool) {
270273
if (!h) {
271274
return false;
272-
} else if (h.is_none()) {
275+
}
276+
if (h.is_none()) {
273277
value = nullptr;
274278
return true;
275279
}
@@ -294,8 +298,7 @@ template <> class type_caster<void> : public type_caster<void_type> {
294298
static handle cast(const void *ptr, return_value_policy /* policy */, handle /* parent */) {
295299
if (ptr)
296300
return capsule(ptr).release();
297-
else
298-
return none().inc_ref();
301+
return none().inc_ref();
299302
}
300303

301304
template <typename T> using cast_op_type = void*&;
@@ -311,9 +314,15 @@ template <> class type_caster<bool> {
311314
public:
312315
bool load(handle src, bool convert) {
313316
if (!src) return false;
314-
else if (src.ptr() == Py_True) { value = true; return true; }
315-
else if (src.ptr() == Py_False) { value = false; return true; }
316-
else if (convert || !std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name)) {
317+
if (src.ptr() == Py_True) {
318+
value = true;
319+
return true;
320+
}
321+
if (src.ptr() == Py_False) {
322+
value = false;
323+
return true;
324+
}
325+
if (convert || !std::strcmp("numpy.bool_", Py_TYPE(src.ptr())->tp_name)) {
317326
// (allow non-implicit conversion for numpy booleans)
318327

319328
Py_ssize_t res = -1;
@@ -337,9 +346,8 @@ template <> class type_caster<bool> {
337346
if (res == 0 || res == 1) {
338347
value = (bool) res;
339348
return true;
340-
} else {
341-
PyErr_Clear();
342349
}
350+
PyErr_Clear();
343351
}
344352
return false;
345353
}
@@ -373,7 +381,8 @@ template <typename StringType, bool IsView = false> struct string_caster {
373381
handle load_src = src;
374382
if (!src) {
375383
return false;
376-
} else if (!PyUnicode_Check(load_src.ptr())) {
384+
}
385+
if (!PyUnicode_Check(load_src.ptr())) {
377386
#if PY_MAJOR_VERSION >= 3
378387
return load_bytes(load_src);
379388
#else
@@ -576,10 +585,11 @@ template <template<typename...> class Tuple, typename... Ts> class tuple_caster
576585
static handle cast(T *src, return_value_policy policy, handle parent) {
577586
if (!src) return none().release();
578587
if (policy == return_value_policy::take_ownership) {
579-
auto h = cast(std::move(*src), policy, parent); delete src; return h;
580-
} else {
581-
return cast(*src, policy, parent);
588+
auto h = cast(std::move(*src), policy, parent);
589+
delete src;
590+
return h;
582591
}
592+
return cast(*src, policy, parent);
583593
}
584594

585595
static constexpr auto name = _("Tuple[") + concat(make_caster<Ts>::name...) + _("]");
@@ -686,14 +696,14 @@ struct copyable_holder_caster : public type_caster_base<type> {
686696
value = v_h.value_ptr();
687697
holder = v_h.template holder<holder_type>();
688698
return true;
689-
} else {
690-
throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
699+
}
700+
throw cast_error("Unable to cast from non-held to held instance (T& to Holder<T>) "
691701
#if defined(NDEBUG)
692-
"(compile in debug mode for type information)");
702+
"(compile in debug mode for type information)");
693703
#else
694-
"of type '" + type_id<holder_type>() + "''");
704+
"of type '"
705+
+ type_id<holder_type>() + "''");
695706
#endif
696-
}
697707
}
698708

699709
template <typename T = holder_type, detail::enable_if_t<!std::is_constructible<T, const T &, type*>::value, int> = 0>
@@ -944,8 +954,7 @@ template <typename T> detail::enable_if_t<detail::move_always<T>::value, T> cast
944954
template <typename T> detail::enable_if_t<detail::move_if_unreferenced<T>::value, T> cast(object &&object) {
945955
if (object.ref_count() > 1)
946956
return cast<T>(object);
947-
else
948-
return move<T>(std::move(object));
957+
return move<T>(std::move(object));
949958
}
950959
template <typename T> detail::enable_if_t<detail::move_never<T>::value, T> cast(object &&object) {
951960
return cast<T>(object);

include/pybind11/chrono.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ template <typename type> class duration_caster {
5353
return true;
5454
}
5555
// If invoked with a float we assume it is seconds and convert
56-
else if (PyFloat_Check(src.ptr())) {
56+
if (PyFloat_Check(src.ptr())) {
5757
value = type(duration_cast<duration<rep, period>>(duration<double>(PyFloat_AsDouble(src.ptr()))));
5858
return true;
5959
}
60-
else return false;
60+
return false;
6161
}
6262

6363
// If this is a duration just return it back

include/pybind11/detail/class.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name
162162
Py_INCREF(descr);
163163
return descr;
164164
}
165-
else {
166-
return PyType_Type.tp_getattro(obj, name);
167-
}
165+
return PyType_Type.tp_getattro(obj, name);
168166
}
169167
#endif
170168

include/pybind11/detail/type_caster_base.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ class type_caster_generic {
670670
return true;
671671
}
672672
// Case 2: We have a derived class
673-
else if (PyType_IsSubtype(srctype, typeinfo->type)) {
673+
if (PyType_IsSubtype(srctype, typeinfo->type)) {
674674
auto &bases = all_type_info(srctype);
675675
bool no_cpp_mi = typeinfo->simple_type;
676676

@@ -687,7 +687,7 @@ class type_caster_generic {
687687
// Case 2b: the python type inherits from multiple C++ bases. Check the bases to see if
688688
// we can find an exact match (or, for a simple C++ type, an inherited match); if so, we
689689
// can safely reinterpret_cast to the relevant pointer.
690-
else if (bases.size() > 1) {
690+
if (bases.size() > 1) {
691691
for (auto base : bases) {
692692
if (no_cpp_mi ? PyType_IsSubtype(base->type, typeinfo->type) : base->type == typeinfo->type) {
693693
this_.load_value(reinterpret_cast<instance *>(src.ptr())->get_value_and_holder(base));

include/pybind11/eigen.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,18 @@ template <typename Type_> struct EigenProps {
169169
return false; // Vector size mismatch
170170
return {rows == 1 ? 1 : n, cols == 1 ? 1 : n, stride};
171171
}
172-
else if (fixed) {
172+
if (fixed) {
173173
// The type has a fixed size, but is not a vector: abort
174174
return false;
175175
}
176-
else if (fixed_cols) {
176+
if (fixed_cols) {
177177
// Since this isn't a vector, cols must be != 1. We allow this only if it exactly
178178
// equals the number of elements (rows is Dynamic, and so 1 row is allowed).
179179
if (cols != n) return false;
180180
return {1, n, stride};
181-
}
182-
else {
183-
// Otherwise it's either fully dynamic, or column dynamic; both become a column vector
181+
} // Otherwise it's either fully dynamic, or column dynamic; both become a column vector
184182
if (fixed_rows && rows != n) return false;
185183
return {n, 1, stride};
186-
}
187184
}
188185

189186
static constexpr bool show_writeable = is_eigen_dense_map<Type>::value && is_eigen_mutable_map<Type>::value;

include/pybind11/functional.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ struct type_caster<std::function<Return(Args...)>> {
9898
auto result = f_.template target<function_type>();
9999
if (result)
100100
return cpp_function(*result, policy).release();
101-
else
102-
return cpp_function(std::forward<Func>(f_), policy).release();
101+
return cpp_function(std::forward<Func>(f_), policy).release();
103102
}
104103

105104
PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ")

include/pybind11/numpy.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,9 +1340,8 @@ template <size_t N> class multi_array_iterator {
13401340
if (++m_index[i] != m_shape[i]) {
13411341
increment_common_iterator(i);
13421342
break;
1343-
} else {
1344-
m_index[i] = 0;
13451343
}
1344+
m_index[i] = 0;
13461345
}
13471346
return *this;
13481347
}
@@ -1493,8 +1492,7 @@ struct vectorize_returned_array {
14931492
static Type create(broadcast_trivial trivial, const std::vector<ssize_t> &shape) {
14941493
if (trivial == broadcast_trivial::f_trivial)
14951494
return array_t<Return, array::f_style>(shape);
1496-
else
1497-
return array_t<Return>(shape);
1495+
return array_t<Return>(shape);
14981496
}
14991497

15001498
static Return *mutable_data(Type &array) {

include/pybind11/pybind11.h

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class cpp_function : public function {
370370
std::memset(rec->def, 0, sizeof(PyMethodDef));
371371
rec->def->ml_name = rec->name;
372372
rec->def->ml_meth
373-
= reinterpret_cast<PyCFunction>(reinterpret_cast<void (*)(void)>(dispatcher));
373+
= reinterpret_cast<PyCFunction>(reinterpret_cast<void (*)()>(dispatcher));
374374
rec->def->ml_flags = METH_VARARGS | METH_KEYWORDS;
375375

376376
capsule rec_capsule(unique_rec.release(), [](void *ptr) {
@@ -898,20 +898,20 @@ class cpp_function : public function {
898898
append_note_if_missing_header_is_suspected(msg);
899899
PyErr_SetString(PyExc_TypeError, msg.c_str());
900900
return nullptr;
901-
} else if (!result) {
901+
}
902+
if (!result) {
902903
std::string msg = "Unable to convert function return value to a "
903904
"Python type! The signature was\n\t";
904905
msg += it->signature;
905906
append_note_if_missing_header_is_suspected(msg);
906907
PyErr_SetString(PyExc_TypeError, msg.c_str());
907908
return nullptr;
908-
} else {
909-
if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) {
910-
auto *pi = reinterpret_cast<instance *>(parent.ptr());
911-
self_value_and_holder.type->init_instance(pi, nullptr);
912-
}
913-
return result.ptr();
914909
}
910+
if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) {
911+
auto *pi = reinterpret_cast<instance *>(parent.ptr());
912+
self_value_and_holder.type->init_instance(pi, nullptr);
913+
}
914+
return result.ptr();
915915
}
916916
};
917917

@@ -1946,9 +1946,9 @@ PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, func
19461946
auto get_arg = [&](size_t n) {
19471947
if (n == 0)
19481948
return ret;
1949-
else if (n == 1 && call.init_self)
1949+
if (n == 1 && call.init_self)
19501950
return call.init_self;
1951-
else if (n <= call.args.size())
1951+
if (n <= call.args.size())
19521952
return call.args[n - 1];
19531953
return handle();
19541954
};
@@ -2272,18 +2272,19 @@ template <class T> function get_override(const T *this_ptr, const char *name) {
22722272
return tinfo ? detail::get_type_override(this_ptr, tinfo, name) : function();
22732273
}
22742274

2275-
#define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \
2276-
do { \
2277-
pybind11::gil_scoped_acquire gil; \
2278-
pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \
2279-
if (override) { \
2280-
auto o = override(__VA_ARGS__); \
2281-
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
2282-
static pybind11::detail::override_caster_t<ret_type> caster; \
2283-
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
2284-
} \
2285-
else return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
2286-
} \
2275+
#define PYBIND11_OVERRIDE_IMPL(ret_type, cname, name, ...) \
2276+
do { \
2277+
pybind11::gil_scoped_acquire gil; \
2278+
pybind11::function override \
2279+
= pybind11::get_override(static_cast<const cname *>(this), name); \
2280+
if (override) { \
2281+
auto o = override(__VA_ARGS__); \
2282+
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
2283+
static pybind11::detail::override_caster_t<ret_type> caster; \
2284+
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \
2285+
} \
2286+
return pybind11::detail::cast_safe<ret_type>(std::move(o)); \
2287+
} \
22872288
} while (false)
22882289

22892290
/** \rst

0 commit comments

Comments
 (0)