An == or != operator compares its own operands with == or !=, which calls itself until the stack overflows

tx0/csharp:recursive-equality-operatorrecommendederrorreliability/equalitydepth fastbug

What it finds

Inside operator ==(Money a, Money b), the test a == null calls the same operator again, which tests a == null again, without end. Comparing references with ReferenceEquals(a, null) or a is null avoids the recursion. The rule reports == and != between a parameter and null inside such an operator, and the operator's own comparison of its two parameters, unless an operand is cast. Only operators that take the declaring type itself are looked at; the runtime declares operators on its primitives whose bodies are the built-in comparison.

From the rule's source, recursive_equality_operator.rs.

Example from the rule's tests
class Money{    public decimal Amount;     public static bool operator ==(Money a, Money b)    {        if (a == null)tx0 reports: a == null calls this operator again        {            return b is null;        }        return !(b is null) && a.Amount == b.Amount;    }     public static bool operator !=(Money a, Money b) => !(a == b);     public override bool Equals(object o) => o is Money m && this == m;     public override int GetHashCode() => Amount.GetHashCode();}
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:recursive-equality-operator" = "error"

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:recursive-equality-operator] reason="..."

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