You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This raises S2259 - "myObject is null on at least one execution path" in the default case (for the myObject.EnumValue dereferencing). But if myObject was null, then myObject?.EnumValue would also be null and the switch would hit the null case, throwing an exception. So default is unreachable if myObject is null.
void Foo()
{
var myObject = GetObjectFromOutside();
switch (myObject?.EnumValue)
{
case MyEnum.One:
throw new MyException("Invalid value.");
case MyEnum.Two:
case null:
throw new MyException("Invalid or null value.");
case MyEnum.Three:
break;
default:
throw new ArgumentOutOfRangeException(
nameof(myObject.EnumValue),
$"Unexpected value ${myObject.EnumValue}");
}
}
Description
The rule does not recognize which cases of a switch statement are safe (all except the one that contains the
null
case).Repro steps
Expected Result
Only the
null
cases should report S2259.Related information
The text was updated successfully, but these errors were encountered: