Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/readability/else-after-return.rst

raw
1 .. title:: clang-tidy - readability-else-after-return
2
3 readability-else-after-return
4 =============================
5
6 `LLVM Coding Standards <https://llvm.org/docs/CodingStandards.html>`_ advises to
7 reduce indentation where possible and where it makes understanding code easier.
8 Early exit is one of the suggested enforcements of that. Please do not use
9 ``else`` or ``else if`` after something that interrupts control flow - like
10 ``return``, ``break``, ``continue``, ``throw``.
11
12 The following piece of code illustrates how the check works. This piece of code:
13
14 .. code-block:: c++
15
16 void foo(int Value) {
17 int Local = 0;
18 for (int i = 0; i < 42; i++) {
19 if (Value == 1) {
20 return;
21 } else {
22 Local++;
23 }
24
25 if (Value == 2)
26 continue;
27 else
28 Local++;
29
30 if (Value == 3) {
31 throw 42;
32 } else {
33 Local++;
34 }
35 }
36 }
37
38
39 Would be transformed into:
40
41 .. code-block:: c++
42
43 void foo(int Value) {
44 int Local = 0;
45 for (int i = 0; i < 42; i++) {
46 if (Value == 1) {
47 return;
48 }
49 Local++;
50
51 if (Value == 2)
52 continue;
53 Local++;
54
55 if (Value == 3) {
56 throw 42;
57 }
58 Local++;
59 }
60 }
61
62 Options
63 -------
64
65 .. option:: WarnOnUnfixable
66
67 When `true`, emit a warning for cases where the check can't output a
68 Fix-It. These can occur with declarations inside the ``else`` branch that
69 would have an extended lifetime if the ``else`` branch was removed.
70 Default value is `true`.
71
72 .. option:: WarnOnConditionVariables
73
74 When `true`, the check will attempt to refactor a variable defined inside
75 the condition of the ``if`` statement that is used in the ``else`` branch
76 defining them just before the ``if`` statement. This can only be done if
77 the ``if`` statement is the last statement in its parent's scope.
78 Default value is `true`.
79
80
81 LLVM alias
82 ----------
83
84 There is an alias of this check called llvm-else-after-return.
85 In that version the options :option:`WarnOnUnfixable` and
86 :option:`WarnOnConditionVariables` are both set to `false` by default.
87
88 This check helps to enforce this `LLVM Coding Standards recommendation
89 <https://llvm.org/docs/CodingStandards.html#don-t-use-else-after-a-return>`_.