CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
69 lines
2.6 KiB
ReStructuredText
69 lines
2.6 KiB
ReStructuredText
.. title:: clang-tidy - bugprone-inc-dec-in-conditions
|
|
|
|
bugprone-inc-dec-in-conditions
|
|
==============================
|
|
|
|
Detects when a variable is both incremented/decremented and referenced inside a
|
|
complex condition and suggests moving them outside to avoid ambiguity in the
|
|
variable's value.
|
|
|
|
When a variable is modified and also used in a complex condition, it can lead
|
|
to unexpected behavior. The side-effect of changing the variable's value within
|
|
the condition can make the code difficult to reason about. Additionally, the
|
|
developer's intended timing for the modification of the variable may not be
|
|
clear, leading to misunderstandings and errors. This can be particularly
|
|
problematic when the condition involves logical operators like ``&&`` and
|
|
``||``, where the order of evaluation can further complicate the situation.
|
|
|
|
Consider the following example:
|
|
|
|
.. code-block:: c++
|
|
|
|
int i = 0;
|
|
// ...
|
|
if (i++ < 5 && i > 0) {
|
|
// do something
|
|
}
|
|
|
|
In this example, the result of the expression may not be what the developer
|
|
intended. The original intention of the developer could be to increment ``i``
|
|
after the entire condition is evaluated, but in reality, i will be incremented
|
|
before ``i > 0`` is executed. This can lead to unexpected behavior and bugs in
|
|
the code. To fix this issue, the developer should separate the increment
|
|
operation from the condition and perform it separately. For example, they can
|
|
increment ``i`` in a separate statement before or after the condition is
|
|
evaluated. This ensures that the value of ``i`` is predictable and consistent
|
|
throughout the code.
|
|
|
|
.. code-block:: c++
|
|
|
|
int i = 0;
|
|
// ...
|
|
i++;
|
|
if (i <= 5 && i > 0) {
|
|
// do something
|
|
}
|
|
|
|
Another common issue occurs when multiple increments or decrements are
|
|
performed on the same variable inside a complex condition. For example:
|
|
|
|
.. code-block:: c++
|
|
|
|
int i = 4;
|
|
// ...
|
|
if (i++ < 5 || --i > 2) {
|
|
// do something
|
|
}
|
|
|
|
There is a potential issue with this code due to the order of evaluation in
|
|
C++. The ``||`` operator used in the condition statement guarantees that if
|
|
the first operand evaluates to ``true``, the second operand will not be
|
|
evaluated. This means that if ``i`` were initially ``4``, the first operand
|
|
``i < 5`` would evaluate to ``true`` and the second operand ``i > 2`` would
|
|
not be evaluated. As a result, the decrement operation ``--i`` would not be
|
|
executed and ``i`` would hold value ``5``, which may not be the intended
|
|
behavior for the developer.
|
|
|
|
To avoid this potential issue, the both increment and decrement operation on
|
|
``i`` should be moved outside the condition statement.
|