We have a dedicated diagnostic UNT0029 to prevent is-null check with UnityEngine.Object. But IDE0270 will suggest to use is-null check over reference equality method.
IDE0270 - Null check can be simplified (if null check)
using UnityEngine;
class Camera : MonoBehaviour
{
public void M()
{
Camera item = FindItem() as Camera;
if (item == null)
throw new System.InvalidOperationException();
}
MonoBehaviour FindItem() => null;
}Under normal circumstances, if (item == null) throw new System.InvalidOperationException() can be simplified to Camera item = FindItem() as Camera ?? throw new System.InvalidOperationException();.
Unity has overridden the == operator for UnityEngine.Object. If you use the == operator to compare a UnityEngine.Object to null, it will return true if the UnityEngine.Object is destroyed, even if the object itself isn't actually null. The ?? operator cannot be overridden in this way, and therefore behaves inconsistently with the == operator, because it checks for null in a different way.