Dropping Curly Braces From Single Statement `guard ... else` Blocks

Sure, you can make diagnostic improvements such as this without a proposal on Swift Evolution!

The existing error message seems fine as-is; a fix-it that adds both the beginning and end braces when the enclosed statement includes an unconditional return seems pretty sensible and a straightforward improvement.

3 Likes

I think this is a great idea.

As a matter in fact, I think statements alike:

if <condition>
   return
while <condition>
{
   // ... some code.

   if <condition>
      break

   // ... some more code
}
if <condition>
   dothis ()
else
   dothat ()

Just read much better and are much clearer than what Swift currently requires, which IMHO is curly bracket overload.

As a minimum it should be able to enable/disable these things via a compiler command line switch.
Or via a option control line somewhere in the source code alike:
#pragma singlelinecurly off

Agreed!

I never suffer from this when I write C++, C, Objective-C, or anything else that allows it.

But I am strong supporter for particular ways of formatting code.

if <condition> dothis () else dothat ()

Should be written as:

if <condition>
   dothis ()
else
   dothat ()

IMHO.

Same with alike constructions.

guard x > y, let z else return

I would write as:

guard
   x > y, let ...
else
   return