USP0021 Prefer reference equality

September 1, 2023 ยท View on GitHub

We have a dedicated diagnostic UNT0029 to prevent is-null check with UnityEngine.Object. But IDE0041 will suggest to use is-null check over reference equality method.

Suppressed Diagnostic ID

IDE0041 - Use 'is null' check

Examples of code that produces a suppressed diagnostic

using UnityEngine;

class Camera : MonoBehaviour
{

    public void Update()
    {
        if (ReferenceEquals(transform, null))
            return;
    }
}

Why is the diagnostic reported?

Under normal circumstances, ReferenceEquals(arg, null) can be simplified to arg is null. But doing that with UnityEngine.Object will trigger UNT0029.

Why do we suppress this diagnostic?

We suppress IDE0041 for that very specific case, because instead you'll have no options for achieving the desired behaviour without suppressing a warning.