Yep, I largely agree with the sentiment that control flow without braces has the potential to be very dangerous which is why I explicitly mentioned that such an improvement would be limited to guard ... else statements.
With an additional restriction that the braces can only be omitted if the else keyword, return keyword, and start of statement must be located on the same line, I think we largely avoid most of these foils.
Namely, the following examples are all allowed:
guard condition else return
guard condition else return value
guard condition
else return
guard condition
else return value
guard
let unwrapped1,
let unwrapped2
else throw ErrorType()
guard
let unwrapped1,
let unwrapped2
else {
self.error = ErrorType()
return
}
guard
let unwrapped1,
let unwrapped2
else
{
self.error = ErrorType()
return
}
guard condition
else
{
return
}
While the following could generate some diagnostic:
guard condition else
return /// <-- Error: Multi-line else clause must be wrapped in curly braces
guard
let unwrapped1,
let unwrapped2
else throw
MyError() /// <-- Error: Multi-line else clause must be wrapped in curly braces
guard condition
else return /// <-- Error: Non-void function should return a value
value /// <-- Warning: Expression of type 'Type' is unused
Since the whole expression must occur with the else keyword, I think that both a) would match formatting best practices for legibility and b) would make accidental edits hard to make, as the compiler would not allow most cases where inserting or re-ordering the line would cause issues, especially since this line would be forced to have else somewhere before its expression.