-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Warnings reported after comparing with object == or != #27928
Comments
Why would the second line not be desired? I would expect the following to be the solution there: -if ((object)x != null) x.ToString(); // warning
+if ((object?)x != null) x.ToString(); |
@sharwell explicit cast should not remove top-level nullability so the inferred state of |
I think the first scenario ( class C
{
static void F(object? x, object y)
{
if (x != null) x.ToString();
if ((object)x != null) x.ToString(); // no warning
if (x == y) x.ToString(); // warning
}
} Update: |
My first instinct is that it should inform nullability, and I suspect that it will be most of our users first instinct as well. |
I'm not sure that we can learn from the static void F(object? x, object y)
{
if (x == y) x.ToString(); // warning is correct
if (y == null) y = "";
} |
I find this unintuitive in the context of the current issue. Do you have a link to explanation of cases where this behavior is expected to produce behavior users are expecting? |
@sharwell I'll try to reconstruct the rationale supporting the current behavior of cast. I just remember it was to help with migrating existing code. A few options:
I don't recall whether there are other options that are sensible. But between those two, (1) is preferable because it allows someone to migrate existing code with the I'll go ahead and close this issue, as we've implemented the specified behavior. |
Warnings are reported for the second and third cases:
[jcouv update:] Besides
object
equality, we should also consider other types (using attribute annotations).The text was updated successfully, but these errors were encountered: