Bluehill.Analyzers

BH0010, BH0011: Do not use equality/inequality operators when comparing spans

When comparing spans, using the equality/inequality operator will compare the addresses of the objects pointed to by the spans, not the contents. If you do not want to compare the addresses directly, you should use pattern matching or SequenceEqual method.

BH0010: Use pattern matching when comparing Span and a constant

When comparing Span and a constant, you should use pattern matching instead of the equality/inequality operators, unless you want to compare what the addresses of the objects pointed to.

BH0011: Use SequenceEqual when comparing Span and a non-constant

When comparing Span and a non-constant, you should use SequenceEqual method instead of the equality/inequality operators, unless you want to directly compare what the addresses of the objects pointed to.

Code with violation

public class TestClass {
    public void TestMethod() {
        var span = "TEST".AsSpan(0, 2);
        var compare1 = span == "TE";
        var chr = 'E';
        var compare2 = span == [ 'T', chr ];
        var ca = [ 'T', 'E' ];
        var compare3 = span == ca;
    }
}

Fixed Code

public class TestClass {
    public void TestMethod() {
        var span = "TEST".AsSpan(0, 2);
        var compare1 = span is "TE";
        var chr = 'E';
        var compare2 = span.SequenceEqual([ 'T', chr ]);
        var ca = [ 'T', 'E' ];
        var compare3 = span.SequenceEqual(ca);
    }
}