xref: /llvm-project/clang-tools-extra/docs/clang-tidy/checks/modernize/use-ranges.rst (revision a4f5e90e319e8bf36ed421a0445218ff17d4de03)
1.. title:: clang-tidy - modernize-use-ranges
2
3modernize-use-ranges
4====================
5
6Detects calls to standard library iterator algorithms that could be replaced
7with a ranges version instead.
8
9Example
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
19Transforms to:
20
21.. code-block:: c++
22
23  auto Iter1 = std::ranges::find(Items, 0);
24  auto AreSame = std::ranges::equal(Items1, Items2);
25
26Supported algorithms
27--------------------
28
29Calls 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
107Note: some range algorithms for ``vector<bool>`` require C++23 because it uses
108proxy iterators.
109
110Reverse Iteration
111-----------------
112
113If calls are made using reverse iterators on containers, The code will be
114fixed using the ``std::views::reverse`` adaptor.
115
116.. code-block:: c++
117
118  auto AreSame = std::equal(Items1.rbegin(), Items1.rend(),
119                            std::crbegin(Items2), std::crend(Items2));
120
121Transforms to:
122
123.. code-block:: c++
124
125  auto AreSame = std::ranges::equal(std::ranges::reverse_view(Items1),
126                                    std::ranges::reverse_view(Items2));
127
128Options
129-------
130
131.. option:: IncludeStyle
132
133   A string specifying which include-style is used, `llvm` or `google`. Default
134   is `llvm`.
135
136.. option:: UseReversePipe
137
138  When `true` (default `false`), fixes which involve reverse ranges will use the
139  pipe adaptor syntax instead of the function syntax.
140
141  .. code-block:: c++
142
143    std::find(Items.rbegin(), Items.rend(), 0);
144
145  Transforms to:
146
147  .. code-block:: c++
148
149    std::ranges::find(Items | std::views::reverse, 0);
150