A type overrides Equals but not GetHashCode, so equal objects may hash differently

tx0/csharp:equals-without-get-hash-coderecommendedwarningreliability/equalitydepth fastbug

What it finds

Hash-based collections find an object by its hash code and then confirm with Equals. Two objects that are equal must have the same hash code; a type that redefines one without the other breaks dictionaries and sets, which then miss keys that are present. The rule reports Equals without GetHashCode; the other way round only makes hashing coarser or finer and stays correct.

From the rule's source, equals_without_get_hash_code.rs.

Example from the rule's tests
class Point{    public int X;     public override bool Equals(object other) => other is Point p && p.X == X;tx0 reports: Point overrides Equals but not GetHashCode} class Size{    public int W;     public override bool Equals(object other) => other is Size s && s.W == W;     public override int GetHashCode() => W;}
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:equals-without-get-hash-code" = "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:equals-without-get-hash-code] reason="..."

tx0 explain tx0/csharp:equals-without-get-hash-code shows the same in the terminal. Found a false positive? Report it.