| 1 |
.. title:: clang-tidy - readability-misleading-indentation |
| 2 |
|
| 3 |
readability-misleading-indentation |
| 4 |
================================== |
| 5 |
|
| 6 |
Correct indentation helps to understand code. Mismatch of the syntactical |
| 7 |
structure and the indentation of the code may hide serious problems. |
| 8 |
Missing braces can also make it significantly harder to read the code, |
| 9 |
therefore it is important to use braces. |
| 10 |
|
| 11 |
The way to avoid dangling else is to always check that an ``else`` belongs |
| 12 |
to the ``if`` that begins in the same column. |
| 13 |
|
| 14 |
You can omit braces when your inner part of e.g. an ``if`` statement has only |
| 15 |
one statement in it. Although in that case you should begin the next statement |
| 16 |
in the same column with the ``if``. |
| 17 |
|
| 18 |
Examples: |
| 19 |
|
| 20 |
.. code-block:: c++ |
| 21 |
|
| 22 |
// Dangling else: |
| 23 |
if (cond1) |
| 24 |
if (cond2) |
| 25 |
foo1(); |
| 26 |
else |
| 27 |
foo2(); // Wrong indentation: else belongs to if(cond2) statement. |
| 28 |
|
| 29 |
// Missing braces: |
| 30 |
if (cond1) |
| 31 |
foo1(); |
| 32 |
foo2(); // Not guarded by if(cond1). |
| 33 |
|
| 34 |
Limitations |
| 35 |
----------- |
| 36 |
|
| 37 |
Note that this check only works as expected when the tabs or spaces are used |
| 38 |
consistently and not mixed. |