-
Notifications
You must be signed in to change notification settings - Fork 77
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
Possible false positive with UNT0008 when nullable type is used #235
Comments
cc @jbevain |
Was going to come here and open an issue as well.
It is therefore safe to use since it is purely a defense against dereferencing a null. You can trivially see this in an example such as https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANgHwAEAmARgFgAoQgZgAIS6BhOgbyrs4fsNKTuAQIuOhAAOMKAEMM0OgF55ACha4AFgGc0zOlE0BKBQD46AMym4NMANwcudzrQZ8BQkeMky5AQmWrN2ix6GobyJuaWNg500U68/LwADHQAKjAaGCp0YKEmYAD8AHQpEADKGFDYAHYA5kr6tpQAvkA=== Where the IL for
|
@tannergooding it is safe to use but using null propagation and null coalescing on Unity objects is inherently ambiguous due to Unity's special handling of null comparisons for destroyed game objects. Our analyzer provides an information here (and not a warning) that you might want to refactor your code to be explicit about the intent. |
@NoTuxNoBux C# nullable types is also something that is going to be ambiguous with regards to Unity's special null handling. Imagine you have: #nullable enable
public static class Foo
{
public static void Bar(GameObject go)
{
go.Destroy();
if (go == null)
{
Debug.Log("GameObject is null");
}
}
} In the method |
I understand what you mean, and I wanted to add some context here: Unity's As Unity's (For reference, there was some related discussion for Roslynator with Unity here: dotnet/roslynator#852.) |
Bug description
UNT0008 (null propagation) also triggers when (explicit) nullable types are used for Unity types.
To Reproduce
Expected behavior
In the case of explicitly nullable types, null propagation can be used validly to check if the object is actually (and only)
null
, much likeis null
andis not null
, all of which disregard the equality operator (intentionally).Additional context
tl;dr: I can argue for both sides of disabling or enabling the warning in the case of an explicitly nullable type, but there seem to always be cases where you either get the warning undesirably, or don't get the warning when you need it.
I'm not entirely sure what the desired behaviour is with explicitly nullable types. It's still a risk to use null propagation because of Unity's equality operator overload, but this may be done intentionally (e.g. initialize-once variable, which doesn't care if the underlying object is destroyed or not). At some point, though, explicitly nullable types will become the default, and if the warning is disabled in this scenario, people might accidentally be using it again.
Still, in a future scenario where every type is explicitly nullable or not at all, comparing a non-nullable
GameObject
through== null
makes little sense; it can't ever be actuallynull
, so you're abusing the== null
to call Unity's operator overload. In this scenario it would probably make sense if Unity replaced it withGameObject.IsDestroyed
or something instead, which makes it implicit the instance exists and you're calling something on it (you already know it's not trulynull
). If it is nullable,is null
and null propagation skip the overload, which is likely intentional, as you said your type could benull
.The text was updated successfully, but these errors were encountered: