-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ConvertTypeCheckPatternToNullCheck
#110973
base: main
Are you sure you want to change the base?
Conversation
Use null check pattern instead of a type check succeeding on any not-null value
Tagging subscribers to this area: @dotnet/area-meta |
@@ -301,7 +301,7 @@ private static Attribute[] InternalParamGetCustomAttributes(ParameterInfo param, | |||
count = 0; | |||
for (int i = 0; i < objAttr.Length; i++) | |||
{ | |||
if (objAttr[i] is object attr) | |||
if (objAttr[i] is { } attr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not readable. In the initial version of pattern matching before is not null
, is { }
is rejected by many projects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot use is not null
with declaration pattern, i.e. if (objAttr[i] is not null attr)
is invalid C#.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (objAttr[i] is { } attr) | |
if (objAttr[i] is not null and var attr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is unfortunate that there are number of more or less cryptic ways one can express a null check in modern C#. The choice between them is a matter of personal preference. My personal preference is the old fashioned:
object? attr = objAttr[i];
if (attr != null)
I believe that it is the best balance of readable and succinct. (Except for the few types that overload operator==. It is fine to use more verbose is not null
in those cases for performance reasons.)
if (Environment.GetEnvironmentVariableCore_NoArrayPool(variable) is { } envVar && | ||
envVar.Length is > 0 and <= 32) // arbitrary limit that allows for some spaces around the maximum length of a non-negative Int32 (10 digits) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Environment.GetEnvironmentVariableCore_NoArrayPool(variable) is { } envVar && | |
envVar.Length is > 0 and <= 32) // arbitrary limit that allows for some spaces around the maximum length of a non-negative Int32 (10 digits) | |
string? envVar = Environment.GetEnvironmentVariableCore_NoArrayPool(variable); | |
if (envVar is { Length: > 0 and <= 32 }) // arbitrary limit that allows for some spaces around the maximum length of a non-negative Int32 (10 digits) | |
@jkotas what is your opinion on this pattern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is about as good as the pattern in main. Some people prefer multiline statements, some people prefer single line statements. (Personally, I tend to prefer single line statements when everything else is equal.)
Use null check pattern instead of a type check succeeding on any not-null value.