From 27374633e5defa26b86bcd6d932e9f77739150e3 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Thu, 7 Jul 2022 21:22:23 +0200 Subject: [PATCH] Don't offer '??=' for pointer types --- ...undCoalesceAssignmentDiagnosticAnalyzer.cs | 7 ++- .../UseCompoundCoalesceAssignmentTests.cs | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundCoalesceAssignmentDiagnosticAnalyzer.cs b/src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundCoalesceAssignmentDiagnosticAnalyzer.cs index b3e9e5ed27283..a54b3856d9231 100644 --- a/src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundCoalesceAssignmentDiagnosticAnalyzer.cs +++ b/src/Analyzers/CSharp/Analyzers/UseCompoundAssignment/CSharpUseCompoundCoalesceAssignmentDiagnosticAnalyzer.cs @@ -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; @@ -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, diff --git a/src/Analyzers/CSharp/Tests/UseCompoundAssignment/UseCompoundCoalesceAssignmentTests.cs b/src/Analyzers/CSharp/Tests/UseCompoundAssignment/UseCompoundCoalesceAssignmentTests.cs index 9b480405e8d68..071bf2571dc3a 100644 --- a/src/Analyzers/CSharp/Tests/UseCompoundAssignment/UseCompoundCoalesceAssignmentTests.cs +++ b/src/Analyzers/CSharp/Tests/UseCompoundAssignment/UseCompoundCoalesceAssignmentTests.cs @@ -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); + } } }