Repository navigation
Closed
dotnet/roslyn-analyzers
#6838Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzer
Milestone
Description
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.
Reacted by Wraith, Huo Yaoyuan, Michael Rumpler, Martin Costello, Ashley, Tan90909090 and Weihan Li
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzer
Activity
EgorBo commentedon Apr 3, 2023
#76735, I thought the plan was to add MemoryExtensions.ReferenceEqual and mark Span.Equals as obsolete/hide from intelisense?
MichaelRumpler commentedon Apr 5, 2023
Maybe also a warning if you set a
Span<T>tonull? Because this is also possible:This is one more reason that users could think
Spanis an object and therefore also the comparison is ok.terrajobst commentedon Apr 5, 2023
@EgorBo
Looks like that got closed in favor of #54794. I'm not sure whether this issue is now a dupe or related.
@GrabYourPitchforks?
@MichaelRumpler
Good point!
terrajobst commentedon Jul 18, 2023
Video
We should warn for any comparison against spans and memories (
==,!=,is,is not):span.IsDefault. Which doesn't exist but we could add one.span.SequenceEqual(...)nulldon't make senseIt seems we don't have consensus on this view point. We should discuss this with once we have quorum again.
terrajobst commentedon Jul 25, 2023
Video
We should warn for any comparison against spans and memories involving the
nullliteral (== null,!= null,is null,is not null). If someone writes== defaultthen they likely meant the default span, which is meaningful and should be allowed.== default.span.IsEmptyCategory: Usage
Severity: Warning