Math.Abs is applied to a hash code, which throws for int.MinValue

tx0/csharp:abs-of-hash-coderecommendedwarningreliability/equalitydepth fastbug

What it finds

A hash code can be any int, int.MinValue included, and Math.Abs(int.MinValue) throws OverflowException because the positive value does not fit. Masking the sign bit, hash & int.MaxValue, or an unsigned remainder gives a non-negative value for every input.

From the rule's source, abs_of_hash_code.rs.

Example from the rule's tests
using System; class Buckets{    int Index(object key, int n) => Math.Abs(key.GetHashCode()) % n;tx0 reports: Math.Abs of a hash code throws when the hash is int.MinValue     int Masked(object key, int n) => (key.GetHashCode() & int.MaxValue) % n;}
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:abs-of-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:abs-of-hash-code] reason="..."

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