Open RichBarton-Arm opened 4 years ago
The use of else
after return
in f18 is, in most instances, quite intentional.
f18 source avoids else
after return
when the return
is handling an exceptional or error case that obviates the remainder of the sequence, and the corresponding if
is not constexpr
. These cases include most (all?) instances where the return
statements have no expressions.
Where the if
condition is constexpr
, an else
block that follows is present because it is semantically necessary to prevent build-time compilation errors.
In situations where an if/else if/else if/else
chain implements a total function, and the predicates of the if
statements are functioning like guards in a function definition (or case
construct) in a functional programming language, f18 style uses a return
in each branch of the construct as a sign that the entire chain is essentially implementing a single return
whose result is determined by a complicated conditional expression. This is a natural representation in C++ of the much more functional style used to good effect throughout f18.
However: where f18 uses else
after return
, the corresponding if
is not constexpr
, and it is possible for control to flow out of the else
block without returning from the enclosing function or lambda, then the else
is not being used properly and should be removed. There should not be many instances of this usage pattern in f18.
LLVM projects have a coding style guideline to strongly discourage the use of else after return. The intention is to simplify the control flow and reduce the indentation level in the code to make it more readable.
Outcomes: