summaryrefslogtreecommitdiffstats
path: root/docs/clang-tidy/checks/readability-else-after-return.rst
blob: c178a6a68ec90a8f28af6a624731680f4dfa56b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
.. title:: clang-tidy - readability-else-after-return

readability-else-after-return
=============================

`LLVM Coding Standards <https://llvm.org/docs/CodingStandards.html>`_ advises to
reduce indentation where possible and where it makes understanding code easier.
Early exit is one of the suggested enforcements of that. Please do not use
``else`` or ``else if`` after something that interrupts control flow - like
``return``, ``break``, ``continue``, ``throw``.

The following piece of code illustrates how the check works. This piece of code:

.. code-block:: c++

    void foo(int Value) {
      int Local = 0;
      for (int i = 0; i < 42; i++) {
        if (Value == 1) {
          return;
        } else {
          Local++;
        }

        if (Value == 2)
          continue;
        else
          Local++;

        if (Value == 3) {
          throw 42;
        } else {
          Local++;
        }
      }
    }


Would be transformed into:

.. code-block:: c++

    void foo(int Value) {
      int Local = 0;
      for (int i = 0; i < 42; i++) {
        if (Value == 1) {
          return;
        }
        Local++;

        if (Value == 2)
          continue;
        Local++;

        if (Value == 3) {
          throw 42;
        }
        Local++;
      }
    }


This check helps to enforce this `LLVM Coding Standards recommendation
<https://llvm.org/docs/CodingStandards.html#don-t-use-else-after-a-return>`_.