A type implements typed equality but keeps the inherited object equality or hash code, so the two disagree

tx0/csharp:equatable-without-object-equalityrecommendedwarningreliability/equalitydepth fastbug

What it finds

IEquatable<T>.Equals is only half of equality. Dictionary<TKey, TValue>, HashSet<T> and LINQ's Distinct take the hash code from GetHashCode, so a type that implements IEquatable<T> and keeps the inherited, identity-based GetHashCode gives equal values different hash codes, and the collections miss keys that are there. object.Equals, non-generic collections and == on object use Equals(object), which then disagrees with Equals(T). A type that implements IEquatable<T> overrides both.

Reported are classes, not partial, not abstract, outside tests and without a base class other than interfaces, that name IEquatable<...> in their base list, declare a public Equals with one parameter, and override neither GetHashCode nor Equals(object), or only one of them. Records get both from the compiler, and the inherited members of structs compare and hash the fields, so both are left alone.

From the rule's source, equatable_without_object_equality.rs.

Example from the rule's tests
using System; public class Point : IEquatable<Point>tx0 reports: Point implements IEquatable but overrides neither Equals(object) nor GetHashCode{    public int X;     public bool Equals(Point other) => other != null && X == other.X;} public class Size : IEquatable<Size>tx0 reports: Size implements IEquatable but does not override GetHashCode{    public int W;     public bool Equals(Size other) => other != null && W == other.W;     public override bool Equals(object obj) => Equals(obj as Size);} public class Color : IEquatable<Color>{    public int R;     public bool Equals(Color other) => other != null && R == other.R;     public override bool Equals(object obj) => Equals(obj as Color);     public override int GetHashCode() => R;} public class Shade : Color, IEquatable<Shade>{    public bool Equals(Shade other) => base.Equals(other);} public struct Cell : IEquatable<Cell>{    public int Row;     public bool Equals(Cell other) => Row == other.Row;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:equatable-without-object-equality" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:equatable-without-object-equality] reason="..."

tx0 explain tx0/csharp:equatable-without-object-equality shows the same in the terminal. Found a false positive? Report it.