Table of Contents

AL0004: Use pattern matching for Span constant comparison

When comparing Span and a constant, use pattern matching instead of equality operators, unless you want to compare addresses.

When it triggers

ReadOnlySpan<char> span = "hello";

// Warning: Use pattern matching instead of equality operators
if (span == "hello") { }  // AL0004

Why this matters

Using == on Span types compares references/addresses, not content. Pattern matching (is) performs content comparison, which is almost always the intended behavior.

How to fix

Use pattern matching:

ReadOnlySpan<char> span = "hello";

if (span is "hello") { }  // Compares content

Configuration

[*.cs]
dotnet_diagnostic.AL0004.severity = warning