40 lines · plain
1.. title:: clang-tidy - readability-misleading-indentation2 3readability-misleading-indentation4==================================5 6Correct indentation helps to understand code. Mismatch of the syntactical7structure and the indentation of the code may hide serious problems.8Missing braces can also make it significantly harder to read the code,9therefore it is important to use braces.10 11The way to avoid dangling else is to always check that an ``else`` belongs12to the ``if`` that begins in the same column.13 14You can omit braces when your inner part of e.g. an ``if`` statement has only15one statement in it. Although in that case you should begin the next statement16in the same column with the ``if``.17 18Examples:19 20.. code-block:: c++21 22 // Dangling else:23 if (cond1)24 if (cond2)25 foo1();26 else27 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 35Limitations36-----------37 38Note that this check only works as expected when the tabs or spaces are used39consistently and not mixed.40