@canton7 mentioned the following in #44109 (comment)
It occurs to me that, provided one side of the == is a constant which has a non-default value, we can skip the & x.HasValue check altogether.
It feels like it would be a nice little win if, when comparing a System.Nullable<T> to a constant with a non-default value, we could avoid generating code to check HasValue. For example: SharpLab
void M(int? x)
{
if (x == 42)
{
System.Console.Write(1);
}
}
Could be lowered to:
void M(int? x)
{
if (x.GetValueOrDefault() == 42)
{
System.Console.Write(1);
}
}
Currently we generate:
void M(Nullable<int> x)
{
Nullable<int> num = x;
int num2 = 42;
if ((num.GetValueOrDefault() == num2) & num.HasValue)
{
Console.Write(1);
}
}
@canton7 mentioned the following in #44109 (comment)
It feels like it would be a nice little win if, when comparing a
System.Nullable<T>to a constant with a non-default value, we could avoid generating code to checkHasValue. For example: SharpLabCould be lowered to:
Currently we generate: