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

Unable to enforce null-checking pattern

Open aybe opened this issue 4 years ago • 2 comments

I want to use the null-checking pattern != null, however, R# always forces me to use is { }:

2021-08-16_07-59-34

When looking at the options, this option is nowhere to be found:

2021-08-16_07-55-52

Is this a bug or is it expected?

aybe avatar Aug 16 '21 06:08 aybe

This has prompted some internal conversations. For some, it's to be expected, for others, it's a surprise. Naturally enough, this is due to Unity's equality operator overrides.

There's a question of whether "checking for null" means doing a reference equality check against null (because we're trying to protect you from a NullReferenceException), or calling custom equality operators that are usually designed for checking equality of two actual objects, not for having distinct semantics with null, and which we can't reason about.

Currently, the "Classic" null check style is following this second option, sees the custom operators and decides it doesn't know what semantics it will introduce and so skips generation. The "Patterns" null check style is next, and generates the code you see.

But there are inconsistencies, where the "Classic" check style is applying the reasoning but other somewhat similar features aren't. The conversation is ongoing and I'll update this when I know more.

citizenmatt avatar Aug 17 '21 08:08 citizenmatt

Alright, thank you for the explanations!

aybe avatar Aug 18 '21 10:08 aybe