|
| 1 | +.. title:: clang-tidy - bugprone-incorrect-iterators |
| 2 | + |
| 3 | +bugprone-incorrect-iterators |
| 4 | +============================ |
| 5 | + |
| 6 | +Detects calls to iterator algorithms where they are called with potentially |
| 7 | +invalid arguments. |
| 8 | + |
| 9 | +Different ranges |
| 10 | +================ |
| 11 | + |
| 12 | +Looks for calls where the range for the ``begin`` argument is different to the |
| 13 | +``end`` argument. |
| 14 | + |
| 15 | +.. code-block:: c++ |
| 16 | + |
| 17 | + std::find(a.begin(), b.end(), 0); |
| 18 | + std::find(std::begin(a), std::end(b)); |
| 19 | + |
| 20 | +Mismatched Begin/End |
| 21 | +==================== |
| 22 | + |
| 23 | +Looks for calls where the ``begin`` parameter is passed as an ``end`` argument or |
| 24 | +vice versa. |
| 25 | + |
| 26 | +.. code-block:: c++ |
| 27 | + |
| 28 | + std::find(a.begin(), a.begin(), 0); // Second argument should be a.end(). |
| 29 | + std::find(a.end(), a.end(), 0); // First argument should be a.begin(). |
| 30 | + |
| 31 | +Container Methods |
| 32 | +================= |
| 33 | + |
| 34 | +Looks for calls to methods on containers that expect an iterator inside the |
| 35 | +container but are given a different container. |
| 36 | + |
| 37 | +.. code-block:: c++ |
| 38 | + |
| 39 | + vec.insert(other.begin(), 5); // The iterator is invalid for this container. |
| 40 | + |
| 41 | +Output Iterators |
| 42 | +================ |
| 43 | + |
| 44 | +Looks for calls which accept a single output iterator but are passed the end of |
| 45 | +a container. |
| 46 | + |
| 47 | +.. code-block:: c++ |
| 48 | + |
| 49 | + std::copy(correct.begin(), correct.end(), incorrect.end()); |
| 50 | + |
| 51 | +Iterator Advancing |
| 52 | +================== |
| 53 | + |
| 54 | +Looks for calls that advance an iterator outside its range. |
| 55 | + |
| 56 | +.. code-block:: c++ |
| 57 | + |
| 58 | + auto Iter = std::next(Cont.end()); |
| 59 | + |
| 60 | +Reverse Iteration |
| 61 | +================= |
| 62 | + |
| 63 | +The check understands ``rbegin`` and ``rend`` and ensures they are in the |
| 64 | +correct places. |
| 65 | + |
| 66 | +.. code-block:: c++ |
| 67 | + |
| 68 | + std::find(a.rbegin(), a.rend(), 0); // OK. |
| 69 | + std::find(a.rend(), a.rbegin(), 0); // Arguments are swapped. |
| 70 | + |
| 71 | +Manually creating a reverse iterator using the ``std::make_reverse_iterator`` is |
| 72 | +also supported, In this case the check looks for calls to ``end`` for the |
| 73 | +``begin`` parameter and vice versa. The name of functions for creating reverse |
| 74 | +iterator can be configured with the option :option:`MakeReverseIterator`. |
| 75 | + |
| 76 | +.. code-block:: c++ |
| 77 | + |
| 78 | + std::find(std::make_reverse_iterator(a.begin()), |
| 79 | + std::make_reverse_iterator(a.end()), 0); // Arguments are swapped. |
| 80 | + std::find(std::make_reverse_iterator(a.end()), |
| 81 | + std::make_reverse_iterator(a.begin()), 0); // OK. |
| 82 | + // Understands this spaghetti looking code is actually doing the correct thing. |
| 83 | + std::find(a.rbegin(), std::make_reverse_iterator(a.begin()), 0); |
| 84 | + |
| 85 | +Options |
| 86 | +------- |
| 87 | + |
| 88 | +.. option:: BeginFree |
| 89 | + |
| 90 | + A semi-colon seperated list of free function names that return an iterator to |
| 91 | + the start of a range. Default value is `::std::begin;std::cbegin`. |
| 92 | + |
| 93 | +.. option:: EndFree |
| 94 | + |
| 95 | + A semi-colon seperated list of free function names that return an iterator to |
| 96 | + the end of a range. Default value is `::std::end;std::cend`. |
| 97 | + |
| 98 | +.. option:: BeginMethod |
| 99 | + |
| 100 | + A semi-colon seperated list of method names that return an iterator to |
| 101 | + the start of a range. Default value is `begin;cbegin`. |
| 102 | + |
| 103 | +.. option:: EndMethod |
| 104 | + |
| 105 | + A semi-colon seperated list of method names that return an iterator to |
| 106 | + the end of a range. Default value is `end;cend`. |
| 107 | + |
| 108 | +.. option:: RBeginFree |
| 109 | + |
| 110 | + A semi-colon seperated list of free function names that return a reverse |
| 111 | + iterator to the start of a range. Default value is `::std::rbegin;std::crbegin`. |
| 112 | + |
| 113 | +.. option:: REndFree |
| 114 | + |
| 115 | + A semi-colon seperated list of free function names that return a reverse |
| 116 | + iterator to the end of a range. Default value is `::std::rend;std::crend`. |
| 117 | + |
| 118 | +.. option:: RBeginMethod |
| 119 | + |
| 120 | + A semi-colon seperated list of method names that return a reverse |
| 121 | + iterator to the start of a range. Default value is `rbegin;crbegin`. |
| 122 | + |
| 123 | +.. option:: REndMethod |
| 124 | + |
| 125 | + A semi-colon seperated list of method names that return a reverse |
| 126 | + iterator to the end of a range. Default value is `rend;crend`. |
| 127 | + |
| 128 | +.. option:: MakeReverseIterator |
| 129 | + |
| 130 | + A semi-colon seperated list of free functions that convert an interator into a |
| 131 | + reverse iterator. Default value is `::std::make_reverse_iterator`. |
0 commit comments