Switch expressions have extra line breaks
There appears to be an extra line break in nested switch expressions, as well as for all default branches
Input:
public class ClassName {
public bool Foo(object entry)
{
return entry switch
{
string s => s.Length switch
{
1 => true,
2 => false,
_ => throw new ArgumentOutOfRangeException("this specific string length is not supported"),
},
int i => i,
_ => throw new ArgumentOutOfRangeException($"entry type {entry.GetType()} not supported"),
};
}
}
Output:
public class ClassName
{
public bool Foo(object entry)
{
return entry switch
{
string s
=> s.Length switch
{
1 => true,
2 => false,
_
=> throw new ArgumentOutOfRangeException(
"this specific string length is not supported"
),
},
int i => i,
_
=> throw new ArgumentOutOfRangeException(
$"entry type {entry.GetType()} not supported"
),
};
}
}
Expected behavior:
public class ClassName {
public bool Foo(object entry)
{
return entry switch
{
string s => s.Length switch
{
1 => true,
2 => false,
_ => throw new ArgumentOutOfRangeException(
"this specific string length is not supported"
),
},
int i => i,
_ => throw new ArgumentOutOfRangeException(
$"entry type {entry.GetType()} not supported"
),
};
}
}
The lines with exceptions end up long enough that they break. Right now when switch arms break, the first break occurs before the => and I don't recall if there was a specific reason it was done that way. Your example does look cleaner, I'll take a look at making this change.
Yup, I agree the exception lines should break, it's just the break being before the => that I find weird. Is that maybe done with the assumption/in the case that the text preceding the arrow is very long? I'm still not sure if that's how I'd want it to look, but I could see that making more sense. e.g.
return s switch {
"some very very long string"
=> "something else very very long",
}
I feel like I'd still prefer the arrow on the same line though 🤔
return s switch {
"some very very long string" =>
"something else very very long",
}
CSharpier has adopted operators at the beginning of lines as opposed to the end, but after saying that I realize that it seems the other instances of => are always at the end of lines when things break. It looks pretty straightforward to change and should really be consistent with the other instances.
public class ClassName
{
public string ShortPropertyName =>
"einairenst_____________________________________________________________________";
public string LongUglyMethod() =>
"einairenst_____________________________________________________________________";
public void DoStuff()
{
CallMethod(o =>
DoThingWith_______________________________________________________________________________(
o
)
);
}
}