Skip to content

Commit

Permalink
Don't offer '??=' for pointer types
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jul 7, 2022
1 parent 46119cb commit 2737463
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Extensions;
Expand Down Expand Up @@ -153,6 +152,12 @@ private void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
return;
}

if (semanticModel.GetTypeInfo(testedExpression, cancellationToken).Type.IsPointerType())
{
// pointers cannot use ??=
return;
}

// Good match.
context.ReportDiagnostic(DiagnosticHelper.Create(
Descriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,5 +694,49 @@ static void Main(object o)
}
}");
}

[WorkItem(62473, "https://github.com/dotnet/roslyn/issues/62473")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestPointerCannotUseCoalesceAssignment()
{
// The purpose of this test is to keep track of whether the language
// allows ??= for pointers in future. It should be kept in 'Preview'.
// If the test failed because language added support and this is no longer
// an error. The behavior for test 'TestPointer' below should be updated as well to suggest ??=
// Note that, when ??= is supported for pointers, the analyzer should check the language version which supports it.
await TestMissingAsync("""
unsafe class Program
{
private static void Main()
{
byte* ptr = null;
{|CS0019:ptr ??= Get()|};
}

static byte* Get() => null;
}
""", LanguageVersion.Preview);
}

[WorkItem(62473, "https://github.com/dotnet/roslyn/issues/62473")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseCompoundAssignment)]
public async Task TestPointer()
{
await TestMissingAsync("""
unsafe class Program
{
private static void Main()
{
byte* ptr = null;
if (ptr is null)
{
ptr = Get();
}
}

static byte* Get() => null;
}
""", LanguageVersion.Preview);
}
}
}

0 comments on commit 2737463

Please sign in to comment.