-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix ConvertTypeCheckPatternToNullCheck
#110973
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -523,7 +523,7 @@ private static bool TryGetInt32EnvironmentVariable(string variable, out int resu | |||||||||||
{ | ||||||||||||
// Avoid globalization stack, as it might in turn be using ArrayPool. | ||||||||||||
|
||||||||||||
if (Environment.GetEnvironmentVariableCore_NoArrayPool(variable) is string envVar && | ||||||||||||
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) | ||||||||||||
Comment on lines
+526
to
527
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
@jkotas what is your opinion on this pattern? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.) |
||||||||||||
{ | ||||||||||||
ReadOnlySpan<char> value = envVar.AsSpan().Trim(' '); | ||||||||||||
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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:
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.)