This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] C# switch expression formatting differs from normal switch formatting
ClosedPublic

Authored by MyDeveloperDay on Dec 13 2021, 1:42 PM.

Details

Summary

https://github.com/llvm/llvm-project/issues/52677

clang-format doesn't format C# switch expressions very well.

int x = a switch {
               1 => (0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0),
               2 => 1,
               _ => 2
           };
int x = a switch { 1 => (0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0), 2 => 1, _ => 2 };

Start with this small use case and try and improve the output. I'll look for other examples to add as tests

Diff Detail

Event Timeline

MyDeveloperDay requested review of this revision.Dec 13 2021, 1:42 PM
MyDeveloperDay created this revision.
curdeius accepted this revision.Dec 13 2021, 11:12 PM

LGTM. AFAIK (which is very limited when it comes to C#), the cases can have also other expressions, not only ints and _. But that can be left for a different patch.
Example from https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching

public State PerformOperation(Operation command) =>
   command switch
   {
       Operation.SystemTest => RunDiagnostics(),
       Operation.Start => StartSystem(),
       Operation.Stop => StopSystem(),
       Operation.Reset => ResetToReady(),
       _ => throw new ArgumentException("Invalid enum value for command", nameof(command)),
   };
This revision is now accepted and ready to land.Dec 13 2021, 11:12 PM

LGTM. AFAIK (which is very limited when it comes to C#), the cases can have also other expressions, not only ints and _. But that can be left for a different patch.
Example from https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching

public State PerformOperation(Operation command) =>
   command switch
   {
       Operation.SystemTest => RunDiagnostics(),
       Operation.Start => StartSystem(),
       Operation.Stop => StopSystem(),
       Operation.Reset => ResetToReady(),
       _ => throw new ArgumentException("Invalid enum value for command", nameof(command)),
   };

I thought that would be the case but actually I tried a load from the Microsoft docs and most where good, it seems to be the numerical ones that seems to be struggling.

This revision was landed with ongoing or failed builds.Dec 15 2021, 11:48 AM
This revision was automatically updated to reflect the committed changes.