Skip to content
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

"Comparison to null is expensive" warning can be misleading #2365

Open
cathei opened this issue Oct 18, 2022 · 3 comments
Open

"Comparison to null is expensive" warning can be misleading #2365

cathei opened this issue Oct 18, 2022 · 3 comments

Comments

@cathei
Copy link

cathei commented Oct 18, 2022

The warning, Avoid null comparisons against UnityEngine.Object subclasses shows for this code:

if (obj != null) DoSomething();

But not showing for this code:

if (obj) DoSomething();

Since bool cast operator of UnityEngine.Object does the same thing as comparing to null, the warning should be shown as same. Otherwise, it can mislead users to use bool cast instead, which is worse practice as it is less explicit.

@NikolayTimofeev
Copy link

NikolayTimofeev commented May 21, 2024

Yeah, they both use the same CompareBaseObjects() inside, no difference in performance.
The warning is misleading for sure.

That's what is inside of them:

    public static bool operator ==(Object x, Object y) => Object.CompareBaseObjects(x, y);
    public static bool operator !=(Object x, Object y) => !Object.CompareBaseObjects(x, y);
    public static implicit operator bool(Object exists)
    {
      return !Object.CompareBaseObjects(exists, (Object) null);
    }

    private static bool CompareBaseObjects(Object lhs, Object rhs)
    {
      bool flag1 = (object) lhs == null;
      bool flag2 = (object) rhs == null;
      if (flag2 & flag1)
        return true;
      if (flag2)
        return !Object.IsNativeObjectAlive(lhs);
      return flag1 ? !Object.IsNativeObjectAlive(rhs) : lhs.m_InstanceID == rhs.m_InstanceID;
    }

@citizenmatt
Copy link
Member

Do you mean the plain underline highlight for performance related info inside Update and related methods?

@NikolayTimofeev
Copy link

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants