roslyn: "Indent open and close braces" doesn't affect switch section block

When “Indent open and close braces” is unchecked:

Actual:

case 5: 
    {
    }

Expected:

case 5: 
{
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 26 (25 by maintainers)

Commits related to this issue

Most upvoted comments

PR ready. Working on tests right now.

Also reported https://developercommunity.visualstudio.com/content/problem/65219/visual-studio-2017-c-switch-case-braces-do-not-pre.html.

Are we sure this isn’t a regression? It feels like braces used to be special cased there.

I’m not sure how frequently users have case statements that are a mix of blocks and not blocks, but “don’t indent if the first statement of a case is a block” is probably the best behavior here. At least, I think it looks the most atheistically pleasing for the case where users are mixing (as you gave in your examples above).

Case statements are the only statement (at least that I can think of right now) where you don’t have to “explicitly” define the block for multi-line statements. So, I think if the user doesn’t define them (At least in my mind), they are effectively there “implicitly”. If the user does define them, then we shouldn’t implicitly define them ourselves

That is, the following case statements should indent “identically”.

case 0:
    Console.WriteLine();
    {
    }
    break;

and

case 0:
{
    Console.WriteLine();
    {
    }
    break;
}

The only case where this breaks down is if the user doesn’t define everything within the block. I think in that case, you don’t indent as well, because it just looks bad otherwise:

case 0:
{
    Console.WriteLine();
}
break;

case 0:
{
    Console.WriteLine();
}
Console.WriteLine();
{
    break;
}

vs

case 0:
{
    Console.WriteLine();
}
    break;

case 0:
{
    Console.WriteLine();
}
    Console.WriteLine();
    {
        break;
    }

There might even be a slightly abandoned PR that fixes this (haven’t dug back into to validate that is the case, but my convo with @CyrusNajmabadi seems to indicate that was the case): https://github.com/dotnet/roslyn/pull/13264