Skip to content

Don't report RCS1172 when checking for null in case of overloaded "==" and "!=" operators #1703

@Trisibo

Description

@Trisibo

The RCS1172 diagnostic ("Use 'is' operator instead of 'as' operator") should not be reported when doing == null or != null comparisons if the target type overloads the == or != operators.

For example, in the case of the Unity game engine, UnityEngine.Object overloads those operators, so when you compare with null it actually checks if the object was "destroyed" by the engine or not, instead of checking whether the actual reference is null. If we have, say, an instance to a UnityEngine.Object as an interface, and we want to check if the object was destroyed, we can do:

IWhatever instance = ...;
if (instance as UnityEngine.Object == null)
...

But that triggers the RCS1172 diagnostic. Doing this instead:

IWhatever instance = ...;
if (instance is not UnityEngine.Object)
...

Would not have the desired effect when instance is not null but the object was destroyed.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions