From c26130ab4be37ac6762f40e8b04e4c968c85954e Mon Sep 17 00:00:00 2001 From: sgrinwis-d2l Date: Fri, 11 Jul 2025 16:39:51 -0400 Subject: [PATCH] Stumbled upon an issue whereby an object array being assigned null causes something to have a null namespace. Reproducible test added. --- .../Generator/AsyncToSyncMethodTransformer.cs | 5 +- .../Async/Generator/SyncGeneratorTests.cs | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/D2L.CodeStyle.Analyzers/Async/Generator/AsyncToSyncMethodTransformer.cs b/src/D2L.CodeStyle.Analyzers/Async/Generator/AsyncToSyncMethodTransformer.cs index d77b904d..91b93e14 100644 --- a/src/D2L.CodeStyle.Analyzers/Async/Generator/AsyncToSyncMethodTransformer.cs +++ b/src/D2L.CodeStyle.Analyzers/Async/Generator/AsyncToSyncMethodTransformer.cs @@ -2,7 +2,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Operations; namespace D2L.CodeStyle.Analyzers.Async.Generator; @@ -111,7 +110,7 @@ private TypeSyntax TransformType( TypeSyntax typeSynt, bool isReturnType = false return typeSynt; } - if( returnTypeInfo.Type.ContainingNamespace.ToString() == "System.Threading.Tasks" ) { + if( returnTypeInfo.Type.ContainingNamespace?.ToString() == "System.Threading.Tasks" ) { switch( returnTypeInfo.Type.MetadataName ) { case "Task": if( isReturnType ) { @@ -134,7 +133,7 @@ private TypeSyntax TransformType( TypeSyntax typeSynt, bool isReturnType = false ); return typeSynt; } - } else if( returnTypeInfo.Type.MetadataName == "IAsyncEnumerable`1" && returnTypeInfo.Type.ContainingNamespace.ToString() == "System.Collections.Generic" ) { + } else if( returnTypeInfo.Type.MetadataName == "IAsyncEnumerable`1" && returnTypeInfo.Type.ContainingNamespace?.ToString() == "System.Collections.Generic" ) { return ( (GenericNameSyntax)typeSynt ) .WithIdentifier( SyntaxFactory.Identifier( "IEnumerable" ) ) .WithTriviaFrom( typeSynt ); diff --git a/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/SyncGeneratorTests.cs b/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/SyncGeneratorTests.cs index 44ad47cf..18e867b6 100644 --- a/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/SyncGeneratorTests.cs +++ b/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/SyncGeneratorTests.cs @@ -75,6 +75,56 @@ partial class Bar { ); } + [Test] + public void OneFileWithOneMethodThatHasObjectArray() { + var result = RunGenerator( @" +using System; +using System.IO; +using System.Threading.Tasks; +using D2L.CodeStyle.Annotations; + +namespace Foo; + +sealed class Bar { + public void Baz() => Console.WriteLine( ""hello"" ); + + [GenerateSync] + public async Task BazAsync( StreamWriter x ) { + Object[] searchThings = null; + return; + +} + + public int Add( int x, int y ) => x + y; +}" + + + ); + + AssertNewTrees( result, @"#pragma warning disable CS1572 +#nullable enable annotations + +using System; +using System.IO; +using System.Threading.Tasks; +using D2L.CodeStyle.Annotations; + +namespace Foo; + +partial class Bar { + + [Blocking] + public void Baz( StreamWriter x ) { + Object[] searchThings = null; + return; + +} +}" + + + ); + } + [Test] public void InterfaceMethod() { var result = RunGenerator( @" @@ -175,6 +225,7 @@ public void Bar() {} ); } + public (Compilation Before, Compilation After) RunGenerator( params string[] sources ) { var oldCompilation = AsyncToSyncMethodTransformerTests.CreateSyncGeneratorTestCompilation( sources.Select( src => CSharpSyntaxTree.ParseText( src ) ).ToArray()