| 1 |
.. title:: clang-tidy - bugprone-inc-dec-in-conditions |
| 2 |
|
| 3 |
bugprone-inc-dec-in-conditions |
| 4 |
============================== |
| 5 |
|
| 6 |
Detects when a variable is both incremented/decremented and referenced inside a |
| 7 |
complex condition and suggests moving them outside to avoid ambiguity in the |
| 8 |
variable's value. |
| 9 |
|
| 10 |
When a variable is modified and also used in a complex condition, it can lead to |
| 11 |
unexpected behavior. The side-effect of changing the variable's value within the |
| 12 |
condition can make the code difficult to reason about. Additionally, the |
| 13 |
developer's intended timing for the modification of the variable may not be |
| 14 |
clear, leading to misunderstandings and errors. This can be particularly |
| 15 |
problematic when the condition involves logical operators like ``&&`` and |
| 16 |
``||``, where the order of evaluation can further complicate the situation. |
| 17 |
|
| 18 |
Consider the following example: |
| 19 |
|
| 20 |
.. code-block:: c++ |
| 21 |
|
| 22 |
int i = 0; |
| 23 |
// ... |
| 24 |
if (i++ < 5 && i > 0) { |
| 25 |
// do something |
| 26 |
} |
| 27 |
|
| 28 |
In this example, the result of the expression may not be what the developer |
| 29 |
intended. The original intention of the developer could be to increment ``i`` |
| 30 |
after the entire condition is evaluated, but in reality, i will be incremented |
| 31 |
before ``i > 0`` is executed. This can lead to unexpected behavior and bugs in |
| 32 |
the code. To fix this issue, the developer should separate the increment |
| 33 |
operation from the condition and perform it separately. For example, they can |
| 34 |
increment ``i`` in a separate statement before or after the condition is |
| 35 |
evaluated. This ensures that the value of ``i`` is predictable and consistent |
| 36 |
throughout the code. |
| 37 |
|
| 38 |
.. code-block:: c++ |
| 39 |
|
| 40 |
int i = 0; |
| 41 |
// ... |
| 42 |
i++; |
| 43 |
if (i <= 5 && i > 0) { |
| 44 |
// do something |
| 45 |
} |
| 46 |
|
| 47 |
Another common issue occurs when multiple increments or decrements are performed |
| 48 |
on the same variable inside a complex condition. For example: |
| 49 |
|
| 50 |
.. code-block:: c++ |
| 51 |
|
| 52 |
int i = 4; |
| 53 |
// ... |
| 54 |
if (i++ < 5 || --i > 2) { |
| 55 |
// do something |
| 56 |
} |
| 57 |
|
| 58 |
There is a potential issue with this code due to the order of evaluation in C++. |
| 59 |
The ``||`` operator used in the condition statement guarantees that if the first |
| 60 |
operand evaluates to ``true``, the second operand will not be evaluated. This |
| 61 |
means that if ``i`` were initially ``4``, the first operand ``i < 5`` would |
| 62 |
evaluate to ``true`` and the second operand ``i > 2`` would not be evaluated. |
| 63 |
As a result, the decrement operation ``--i`` would not be executed and ``i`` |
| 64 |
would hold value ``5``, which may not be the intended behavior for the developer. |
| 65 |
|
| 66 |
To avoid this potential issue, the both increment and decrement operation on |
| 67 |
``i`` should be moved outside the condition statement. |