| 1 |
.. title:: clang-tidy - bugprone-assignment-in-if-condition |
| 2 |
|
| 3 |
bugprone-assignment-in-if-condition |
| 4 |
=================================== |
| 5 |
|
| 6 |
Finds assignments within conditions of `if` statements. |
| 7 |
Such assignments are bug-prone because they may have been intended as equality tests. |
| 8 |
|
| 9 |
This check finds all assignments within `if` conditions, including ones that are not flagged |
| 10 |
by `-Wparentheses` due to an extra set of parentheses, and including assignments that call |
| 11 |
an overloaded `operator=()`. The identified assignments violate |
| 12 |
`BARR group "Rule 8.2.c" <https://barrgroup.com/embedded-systems/books/embedded-c-coding-standard/statement-rules/if-else-statements>`_. |
| 13 |
|
| 14 |
.. code-block:: c++ |
| 15 |
|
| 16 |
int f = 3; |
| 17 |
if(f = 4) { // This is identified by both `Wparentheses` and this check - should it have been: `if (f == 4)` ? |
| 18 |
f = f + 1; |
| 19 |
} |
| 20 |
|
| 21 |
if((f == 5) || (f = 6)) { // the assignment here `(f = 6)` is identified by this check, but not by `-Wparentheses`. Should it have been `(f == 6)` ? |
| 22 |
f = f + 2; |
| 23 |
} |