rspec/rules/S1698/csharp/rule.adoc

81 lines
2.2 KiB
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
Using the equality ``++==++`` and inequality ``++!=++`` operators to compare two objects generally works. The operators can be overloaded, and therefore the comparison can resolve to the appropriate method. However, when the operators are used on interface instances, then ``++==++`` resolves to reference equality, which may result in unexpected behavior if implementing classes override ``++Equals++``. Similarly, when a class overrides ``++Equals++``, but instances are compared with non-overloaded ``++==++``, there is a high chance that value comparison was meant instead of the reference one.
2020-06-30 12:47:33 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public interface IMyInterface
{
}
public class MyClass : IMyInterface
{
public override bool Equals(object obj)
{
//...
}
}
public class Program
{
public static void Method(IMyInterface instance1, IMyInterface instance2)
{
if (instance1 == instance2) // Noncompliant, will do reference equality check, but was that intended? MyClass overrides Equals.
{
Console.WriteLine("Equal");
}
}
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
public interface IMyInterface
{
}
public class MyClass : IMyInterface
{
public override bool Equals(object obj)
{
//...
}
}
public class Program
{
public static void Method(IMyInterface instance1, IMyInterface instance2)
{
if (object.Equals(instance1, instance2)) // object.Equals checks for null and then calls the instance based Equals, so MyClass.Equals
{
Console.WriteLine("Equal");
}
}
}
----
== Exceptions
2021-01-27 13:42:22 +01:00
The rule does not report on comparisons of ``++System.Type++`` instances and on comparisons inside ``++Equals++`` overrides.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
It also does not raise an issue when one of the operands is ``++null++`` nor when one of the operand is cast to ``++object++`` (because in this case we want to ensure reference equality even if some ``++==++`` overload is present).
2020-06-30 12:47:33 +02:00
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]