-
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
Consider 'value is null' instead of 'value == null' for the "Add null check" codefix #24237
Comments
This is minor (and more of a detail that needs to be considered once this gets implemented), but
does not compile, while |
We should change the language to permit |
For the sake of completeness, there are some more inconsistencies here (but some might be unresolvable): public class C {
//unconstraint
public void UnconstraintIs<T>(T v) {
var b = v is null; //error CS0403: Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead.
}
public void UnconstraintNull<T>(T v) {
var b = v == null; // Fine
}
//class
public void RefconstraintIs<T>(T v) where T:class {
var b = v is null; //Fine
}
public void RefconstraintNull<T>(T v) where T:class {
var b = v == null; //Fine
}
//struct
public void ValueconstraintIs<T>(T v) where T:struct {
var b = v is null; //error CS0403: Cannot convert null to type parameter 'T' because it could be a non-nullable value type. Consider using 'default(T)' instead.
}
public void ValueconstraintNull<T>(T v) where T:struct {
var b = v == null; //error CS0019: Operator '==' cannot be applied to operands of type 'T' and '<null>'
}
public void ValueconstraintRefEquals<T>(T v) where T:struct {
var b = ReferenceEquals(v, null); // Fine
}
} Public Class C
Public Sub UnconstraintIs(Of T)(v As T)
Dim flag As Boolean = v Is Nothing
End Sub
Public Sub RefconstraintIs(Of T As Class)(v As T)
Dim flag As Boolean = v Is Nothing
End Sub
Public Sub ValueconstraintIs(Of T As Structure)(v As T)
Dim flag As Boolean = v Is Nothing ' error BC30020: 'Is' operator does not accept operands of type 'T'. Operands must be reference or nullable types.
End Sub
Public Sub ValueconstraintReferenceEquals(Of T As Structure)(v As T)
Dim flag As Boolean = ReferenceEquals(v, Nothing)
End Sub
End Class |
Imo, if the user has an overloaded operator == then we should be respecting that. I think using "== null" is appropriate for that feature. |
also reported here |
Could this be implemented and configured using an option in .editorconfig? Autogenerating code increases productivity (and I'm talking not only about this specific code fix, but also about auto-generated constructors), yet if one wants to enforce the |
It's possible that there could be a custom operator defined for
==
, which could be slower than a simple pointer comparison.is null
reads better and is guaranteed to be just a pointer comparison.This may warrant looking into the user's code style to see if they already have a lot of
== null
s oris null
s, to see which codefix should be offered to maintain consistency.The text was updated successfully, but these errors were encountered: