Skip to content

Warn when comparing spans to null #84265

Closed
dotnet/roslyn-analyzers
#6838

Description

@terrajobst

The following code

var array = new char[0];
var span = array.AsSpan();
var emptyArray = Array.Empty<char>();
var emptySpan = Span<char>.Empty;

if (array == null)
    Console.WriteLine("array is null");
else
    Console.WriteLine("array is not null");

if (span == null)
    Console.WriteLine("span is null");
else
    Console.WriteLine("span is not null");

if (emptyArray == null)
    Console.WriteLine("emptyArray is null");
else
    Console.WriteLine("emptyArray is not null");

if (emptySpan == null)
    Console.WriteLine("emptySpan is null");
else
    Console.WriteLine("emptySpan is not null");

outputs the following:

array is not null
span is not null
emptyArray is not null
emptySpan is null

The only surprising one is Span<char>.Empty == null resulting in true.

Spans can be compared to null because there is an implicit conversion from arrays to spans and the null literal can be converted to an array, so the compiler ends up emitting this:

if (Span<char>.op_Equals(emptySpan, (Span<char>)((char[])null)))
    Console.WriteLine("emptySpan is null");
else
    Console.WriteLine("emptySpan is not null");

The empty span is defined as the null literal converted to a span, so this resulting in true is, albeit surprising, sensible and by design.

This effectively hides the built-in diagnostic CS8073 against comparing value types to null. We should consider adding a diagnostic for comparisons between spans and null.

As pointed out, this should probably also result in a warning:

Span<Cell> line = null;

More context on Twitter.

Activity

  1. added
    api-suggestion
    code-analyzer
    on Apr 3, 2023
  2. ghost added
    untriaged
    on Apr 3, 2023
  3. EgorBo commented on Apr 3, 2023

    @EgorBo
    Member

    #76735, I thought the plan was to add MemoryExtensions.ReferenceEqual and mark Span.Equals as obsolete/hide from intelisense?

  4. MichaelRumpler commented on Apr 5, 2023

    @MichaelRumpler

    Maybe also a warning if you set a Span<T> to null? Because this is also possible:

    Span<Cell> line = null;
    

    This is one more reason that users could think Span is an object and therefore also the comparison is ok.

  5. terrajobst commented on Apr 5, 2023

    @terrajobst
    ContributorAuthor

    @EgorBo

    #76735, I thought the plan was to add MemoryExtensions.ReferenceEqual and mark Span.Equals as obsolete/hide from intelisense?

    Looks like that got closed in favor of #54794. I'm not sure whether this issue is now a dupe or related.

    @GrabYourPitchforks?

    @MichaelRumpler

    Maybe also a warning if you set a Span<T> to null?

    Good point!

  6. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on May 26, 2023
  7. terrajobst commented on Jul 18, 2023

    @terrajobst
    ContributorAuthor

    Video

    We should warn for any comparison against spans and memories (==, !=, is, is not):

    • Comparisons against the default span should be written as span.IsDefault. Which doesn't exist but we could add one.
    • Comparisons against content should be done as span.SequenceEqual(...)
    • Comparisons against null don't make sense

    It seems we don't have consensus on this view point. We should discuss this with once we have quorum again.

  8. added
    api-needs-work
    api-ready-for-review
    and removed
    api-ready-for-review
    api-needs-work
    on Jul 18, 2023
  9. terrajobst commented on Jul 25, 2023

    @terrajobst
    ContributorAuthor

    Video

    We should warn for any comparison against spans and memories involving the null literal (== null, != null, is null, is not null). If someone writes == default then they likely meant the default span, which is meaningful and should be allowed.

    • Comparisons against the default span should be written as == default.
    • Asking whether the span is empty should be written as span.IsEmpty

    Category: Usage
    Severity: Warning

  10. added
    api-approved
    and removed
    api-ready-for-review
    on Jul 25, 2023
  11. added
    needs-further-triage
    on Aug 14, 2023
  12. added this to the Future milestone on Aug 14, 2023
  13. locked and limited conversation to collaborators on Apr 4, 2024
  14. removed
    needs-further-triage
    on Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Runtimecode-analyzer

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions