Skip to content

Commit ae07d4c

Browse files
authored
maint(Clang-Tidy): readability-const-return (#3254)
* Enable clang-tidy readability-const-return * PyTest functional * Fix regression * Fix actual regression * Remove one more NOLINT * Update comment
1 parent 4d5ad03 commit ae07d4c

File tree

6 files changed

+18
-9
lines changed

6 files changed

+18
-9
lines changed

.clang-tidy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ modernize-use-override,
3131
modernize-use-using,
3232
*performance*,
3333
readability-avoid-const-params-in-decls,
34+
readability-const-return-type,
3435
readability-container-size-empty,
35-
readability-else-after-return,
3636
readability-delete-null-pointer,
37+
readability-else-after-return,
3738
readability-implicit-bool-conversion,
3839
readability-make-member-function-const,
3940
readability-misplaced-array-index,

include/pybind11/cast.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,13 +1194,15 @@ class argument_loader {
11941194
}
11951195

11961196
template <typename Return, typename Guard, typename Func>
1197+
// NOLINTNEXTLINE(readability-const-return-type)
11971198
enable_if_t<!std::is_void<Return>::value, Return> call(Func &&f) && {
1198-
return std::move(*this).template call_impl<Return>(std::forward<Func>(f), indices{}, Guard{});
1199+
return std::move(*this).template call_impl<remove_cv_t<Return>>(std::forward<Func>(f), indices{}, Guard{});
11991200
}
12001201

12011202
template <typename Return, typename Guard, typename Func>
1203+
// NOLINTNEXTLINE(readability-const-return-type)
12021204
enable_if_t<std::is_void<Return>::value, void_type> call(Func &&f) && {
1203-
std::move(*this).template call_impl<Return>(std::forward<Func>(f), indices{}, Guard{});
1205+
std::move(*this).template call_impl<remove_cv_t<Return>>(std::forward<Func>(f), indices{}, Guard{});
12041206
return void_type();
12051207
}
12061208

@@ -1222,6 +1224,7 @@ class argument_loader {
12221224
}
12231225

12241226
template <typename Return, typename Func, size_t... Is, typename Guard>
1227+
// NOLINTNEXTLINE(readability-const-return-type)
12251228
Return call_impl(Func &&f, index_sequence<Is...>, Guard &&) && {
12261229
return std::forward<Func>(f)(cast_op<Args>(std::move(std::get<Is>(argcasters)))...);
12271230
}

include/pybind11/detail/descr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ constexpr enable_if_t<B, T1> _(const T1 &d, const T2 &) { return d; }
7777
template <bool B, typename T1, typename T2>
7878
constexpr enable_if_t<!B, T2> _(const T1 &, const T2 &d) { return d; }
7979

80-
template <size_t Size> auto constexpr _() -> decltype(int_to_str<Size / 10, Size % 10>::digits) {
80+
template <size_t Size>
81+
auto constexpr _() -> remove_cv_t<decltype(int_to_str<Size / 10, Size % 10>::digits)> {
8182
return int_to_str<Size / 10, Size % 10>::digits;
8283
}
8384

include/pybind11/pybind11.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,13 +2002,13 @@ template <return_value_policy Policy = return_value_policy::reference_internal,
20022002
typename KeyType = decltype((*std::declval<Iterator>()).first),
20032003
#endif
20042004
typename... Extra>
2005-
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&... extra) {
2005+
iterator make_key_iterator(Iterator first, Sentinel last, Extra &&...extra) {
20062006
using state = detail::iterator_state<Iterator, Sentinel, true, Policy>;
20072007

20082008
if (!detail::get_type_info(typeid(state), false)) {
20092009
class_<state>(handle(), "iterator", pybind11::module_local())
20102010
.def("__iter__", [](state &s) -> state& { return s; })
2011-
.def("__next__", [](state &s) -> KeyType {
2011+
.def("__next__", [](state &s) -> detail::remove_cv_t<KeyType> {
20122012
if (!s.first_or_done)
20132013
++s.it;
20142014
else

include/pybind11/pytypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ class sequence_fast_readonly {
773773
protected:
774774
using iterator_category = std::random_access_iterator_tag;
775775
using value_type = handle;
776-
using reference = const handle;
776+
using reference = handle;
777777
using pointer = arrow_proxy<const handle>;
778778

779779
sequence_fast_readonly(handle obj, ssize_t n) : ptr(PySequence_Fast_ITEMS(obj.ptr()) + n) { }
@@ -816,7 +816,7 @@ class dict_readonly {
816816
protected:
817817
using iterator_category = std::forward_iterator_tag;
818818
using value_type = std::pair<handle, handle>;
819-
using reference = const value_type;
819+
using reference = value_type;
820820
using pointer = arrow_proxy<const value_type>;
821821

822822
dict_readonly() = default;
@@ -966,7 +966,7 @@ class iterator : public object {
966966
using iterator_category = std::input_iterator_tag;
967967
using difference_type = ssize_t;
968968
using value_type = handle;
969-
using reference = const handle;
969+
using reference = handle;
970970
using pointer = const handle *;
971971

972972
PYBIND11_OBJECT_DEFAULT(iterator, object, PyIter_Check)

tests/test_eigen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ TEST_SUBMODULE(eigen, m) {
178178
ReturnTester() { print_created(this); }
179179
~ReturnTester() { print_destroyed(this); }
180180
static Eigen::MatrixXd create() { return Eigen::MatrixXd::Ones(10, 10); }
181+
// NOLINTNEXTLINE(readability-const-return-type)
181182
static const Eigen::MatrixXd createConst() { return Eigen::MatrixXd::Ones(10, 10); }
182183
Eigen::MatrixXd &get() { return mat; }
183184
Eigen::MatrixXd *getPtr() { return &mat; }
@@ -244,6 +245,9 @@ TEST_SUBMODULE(eigen, m) {
244245

245246
// test_fixed, and various other tests
246247
m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); });
248+
// Our Eigen does a hack which respects constness through the numpy writeable flag.
249+
// Therefore, the const return actually affects this type despite being an rvalue.
250+
// NOLINTNEXTLINE(readability-const-return-type)
247251
m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); });
248252
m.def("fixed_c", [mat]() -> FixedMatrixC { return FixedMatrixC(mat); });
249253
m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; });

0 commit comments

Comments
 (0)