resharper-unity icon indicating copy to clipboard operation
resharper-unity copied to clipboard

Suggestion to use "is { }" pattern for null checking is wrong.

Open aybe opened this issue 4 years ago • 3 comments

Take this piece of code:

private void OnDrawGizmosSelected()
{
    Gizmos.DrawSphere(rb.position, 1.0f);
}

Check expression for null changes it to this which doesn't work for Unity objects:

private void OnDrawGizmosSelected()
{
    if (rb is { })
        Gizmos.DrawSphere(rb.position, 1.0f);
}

Instead it should do this:

private void OnDrawGizmosSelected()
{
    if (rb != null)
        Gizmos.DrawSphere(rb.position, 1.0f);
}

Now that's even more problematic because nowhere in the options this can be changed.

JetBrains ReSharper 2020.3 Build 203.0.20201211.113035 Unity Support 2020.3.1.132

aybe avatar Dec 31 '20 00:12 aybe

Is this using C# 8? Does the project compile correctly (with if (rb is { })) in Unity?

Order of generated null checks can be specified in the "Null Checking" tab of Preferences | Editor | Code Style | C#, but classic (if (arg == null)) should be first.

citizenmatt avatar Jan 07 '21 16:01 citizenmatt

The project compiles properly, rather, it's a usability problem, this syntax isn't correct because Unity circumvents some operators.

See the following example, b should be True in this case:

devenv_2021-01-08_11-01-38

For the null checking syntax, it's a bit unfortunate, I triple checked and indeed it was the case, but ending up reinstalling R# auto-magically fixed it. 🤣

aybe avatar Jan 08 '21 10:01 aybe

I also have this problem and I don't know how to fix this in Preferences | Editor | Code Style | C#. The settings seem to only work for null assertions and not null checks. Am I missing something?

pnarimani avatar May 25 '21 19:05 pnarimani