| 1 |
.. title:: clang-tidy - modernize-use-ranges |
| 2 |
|
| 3 |
modernize-use-ranges |
| 4 |
==================== |
| 5 |
|
| 6 |
Detects calls to standard library iterator algorithms that could be replaced |
| 7 |
with a ranges version instead. |
| 8 |
|
| 9 |
Example |
| 10 |
------- |
| 11 |
|
| 12 |
.. code-block:: c++ |
| 13 |
|
| 14 |
auto Iter1 = std::find(Items.begin(), Items.end(), 0); |
| 15 |
auto AreSame = std::equal(Items1.cbegin(), Items1.cend(), |
| 16 |
std::begin(Items2), std::end(Items2)); |
| 17 |
|
| 18 |
|
| 19 |
Transforms to: |
| 20 |
|
| 21 |
.. code-block:: c++ |
| 22 |
|
| 23 |
auto Iter1 = std::ranges::find(Items, 0); |
| 24 |
auto AreSame = std::ranges::equal(Items1, Items2); |
| 25 |
|
| 26 |
Supported algorithms |
| 27 |
-------------------- |
| 28 |
|
| 29 |
Calls to the following std library algorithms are checked: |
| 30 |
|
| 31 |
``std::adjacent_find``, |
| 32 |
``std::all_of``, |
| 33 |
``std::any_of``, |
| 34 |
``std::binary_search``, |
| 35 |
``std::copy_backward``, |
| 36 |
``std::copy_if``, |
| 37 |
``std::copy``, |
| 38 |
``std::destroy``, |
| 39 |
``std::equal_range``, |
| 40 |
``std::equal``, |
| 41 |
``std::fill``, |
| 42 |
``std::find_end``, |
| 43 |
``std::find_if_not``, |
| 44 |
``std::find_if``, |
| 45 |
``std::find``, |
| 46 |
``std::for_each``, |
| 47 |
``std::generate``, |
| 48 |
``std::includes``, |
| 49 |
``std::inplace_merge``, |
| 50 |
``std::iota``, |
| 51 |
``std::is_heap_until``, |
| 52 |
``std::is_heap``, |
| 53 |
``std::is_partitioned``, |
| 54 |
``std::is_permutation``, |
| 55 |
``std::is_sorted_until``, |
| 56 |
``std::is_sorted``, |
| 57 |
``std::lexicographical_compare``, |
| 58 |
``std::lower_bound``, |
| 59 |
``std::make_heap``, |
| 60 |
``std::max_element``, |
| 61 |
``std::merge``, |
| 62 |
``std::min_element``, |
| 63 |
``std::minmax_element``, |
| 64 |
``std::mismatch``, |
| 65 |
``std::move_backward``, |
| 66 |
``std::move``, |
| 67 |
``std::next_permutation``, |
| 68 |
``std::none_of``, |
| 69 |
``std::partial_sort_copy``, |
| 70 |
``std::partition_copy``, |
| 71 |
``std::partition_point``, |
| 72 |
``std::partition``, |
| 73 |
``std::pop_heap``, |
| 74 |
``std::prev_permutation``, |
| 75 |
``std::push_heap``, |
| 76 |
``std::remove_copy_if``, |
| 77 |
``std::remove_copy``, |
| 78 |
``std::remove``, ``std::remove_if``, |
| 79 |
``std::replace_if``, |
| 80 |
``std::replace``, |
| 81 |
``std::reverse_copy``, |
| 82 |
``std::reverse``, |
| 83 |
``std::rotate``, |
| 84 |
``std::rotate_copy``, |
| 85 |
``std::sample``, |
| 86 |
``std::search``, |
| 87 |
``std::set_difference``, |
| 88 |
``std::set_intersection``, |
| 89 |
``std::set_symmetric_difference``, |
| 90 |
``std::set_union``, |
| 91 |
``std::shift_left``, |
| 92 |
``std::shift_right``, |
| 93 |
``std::sort_heap``, |
| 94 |
``std::sort``, |
| 95 |
``std::stable_partition``, |
| 96 |
``std::stable_sort``, |
| 97 |
``std::transform``, |
| 98 |
``std::uninitialized_copy``, |
| 99 |
``std::uninitialized_default_construct``, |
| 100 |
``std::uninitialized_fill``, |
| 101 |
``std::uninitialized_move``, |
| 102 |
``std::uninitialized_value_construct``, |
| 103 |
``std::unique_copy``, |
| 104 |
``std::unique``, |
| 105 |
``std::upper_bound``. |
| 106 |
|
| 107 |
Reverse Iteration |
| 108 |
----------------- |
| 109 |
|
| 110 |
If calls are made using reverse iterators on containers, The code will be |
| 111 |
fixed using the ``std::views::reverse`` adaptor. |
| 112 |
|
| 113 |
.. code-block:: c++ |
| 114 |
|
| 115 |
auto AreSame = std::equal(Items1.rbegin(), Items1.rend(), |
| 116 |
std::crbegin(Items2), std::crend(Items2)); |
| 117 |
|
| 118 |
Transforms to: |
| 119 |
|
| 120 |
.. code-block:: c++ |
| 121 |
|
| 122 |
auto AreSame = std::ranges::equal(std::ranges::reverse_view(Items1), |
| 123 |
std::ranges::reverse_view(Items2)); |
| 124 |
|
| 125 |
Options |
| 126 |
------- |
| 127 |
|
| 128 |
.. option:: IncludeStyle |
| 129 |
|
| 130 |
A string specifying which include-style is used, `llvm` or `google`. Default |
| 131 |
is `llvm`. |
| 132 |
|
| 133 |
.. option:: UseReversePipe |
| 134 |
|
| 135 |
When `true` (default `false`), fixes which involve reverse ranges will use the |
| 136 |
pipe adaptor syntax instead of the function syntax. |
| 137 |
|
| 138 |
.. code-block:: c++ |
| 139 |
|
| 140 |
std::find(Items.rbegin(), Items.rend(), 0); |
| 141 |
|
| 142 |
Transforms to: |
| 143 |
|
| 144 |
.. code-block:: c++ |
| 145 |
|
| 146 |
std::ranges::find(Items | std::views::reverse, 0); |