| 1 |
.. title:: clang-tidy - bugprone-infinite-loop |
| 2 |
|
| 3 |
bugprone-infinite-loop |
| 4 |
====================== |
| 5 |
|
| 6 |
Finds obvious infinite loops (loops where the condition variable is not changed |
| 7 |
at all). |
| 8 |
|
| 9 |
Finding infinite loops is well-known to be impossible (halting problem). |
| 10 |
However, it is possible to detect some obvious infinite loops, for example, if |
| 11 |
the loop condition is not changed. This check detects such loops. A loop is |
| 12 |
considered infinite if it does not have any loop exit statement (``break``, |
| 13 |
``continue``, ``goto``, ``return``, ``throw`` or a call to a function called as |
| 14 |
``[[noreturn]]``) and all of the following conditions hold for every variable in |
| 15 |
the condition: |
| 16 |
|
| 17 |
- It is a local variable. |
| 18 |
- It has no reference or pointer aliases. |
| 19 |
- It is not a structure or class member. |
| 20 |
|
| 21 |
Furthermore, the condition must not contain a function call to consider the loop |
| 22 |
infinite since functions may return different values for different calls. |
| 23 |
|
| 24 |
For example, the following loop is considered infinite `i` is not changed in |
| 25 |
the body: |
| 26 |
|
| 27 |
.. code-block:: c++ |
| 28 |
|
| 29 |
int i = 0, j = 0; |
| 30 |
while (i < 10) { |
| 31 |
++j; |
| 32 |
} |