From 5c0e2d72a0f4728e7d87773569930685d6259a18 Mon Sep 17 00:00:00 2001 From: Jacob Parker Date: Tue, 13 Feb 2024 17:25:28 -0500 Subject: [PATCH] Emit a nullable directive in generated sync code (#947) --- .../Async/Generator/FileCollector.cs | 3 +++ .../Async/Generator/FileCollectorTests.cs | 6 ++++++ .../Async/Generator/SyncGeneratorTests.cs | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/src/D2L.CodeStyle.Analyzers/Async/Generator/FileCollector.cs b/src/D2L.CodeStyle.Analyzers/Async/Generator/FileCollector.cs index 54bab64c..f4f5b9fa 100644 --- a/src/D2L.CodeStyle.Analyzers/Async/Generator/FileCollector.cs +++ b/src/D2L.CodeStyle.Analyzers/Async/Generator/FileCollector.cs @@ -82,6 +82,9 @@ public static FileCollector Create( public string CollectSource() { // TODO: Remove this and modify XML param elements in generator when changed/removed m_out.AppendLine( "#pragma warning disable CS1572" ); + // This allows us to copy+paste annotations but otherwise does not emit diagnostics + // otherwise we get "CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source." + m_out.AppendLine( "#nullable annotations" ); // File-scoped usings: m_out.Append( m_root.Usings.ToFullString() ); diff --git a/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/FileCollectorTests.cs b/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/FileCollectorTests.cs index 4559b512..6d7faacb 100644 --- a/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/FileCollectorTests.cs +++ b/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/FileCollectorTests.cs @@ -24,6 +24,7 @@ void SomeMethod() {} ); Assert.AreEqual( @"#pragma warning disable CS1572 +#nullable annotations ", collector.CollectSource() ); } @@ -52,6 +53,7 @@ void MyMethodBefore() { ); Assert.AreEqual( @"#pragma warning disable CS1572 +#nullable annotations using Foo; @@ -89,6 +91,7 @@ void MyMethodBefore() { ); Assert.AreEqual( @"#pragma warning disable CS1572 +#nullable annotations using Foo; @@ -124,6 +127,7 @@ void MyMethodBefore() {{ ); Assert.AreEqual( @$"#pragma warning disable CS1572 +#nullable annotations partial {kind} X {{ any text @@ -163,6 +167,7 @@ class Ignored2 {} ); Assert.AreEqual( @"#pragma warning disable CS1572 +#nullable annotations using Foo; @@ -244,6 +249,7 @@ void MyBeforeMethod4() { } ); Assert.AreEqual( @"#pragma warning disable CS1572 +#nullable annotations using Foo; diff --git a/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/SyncGeneratorTests.cs b/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/SyncGeneratorTests.cs index 783c4c65..b467d769 100644 --- a/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/SyncGeneratorTests.cs +++ b/tests/D2L.CodeStyle.Analyzers.Test/Async/Generator/SyncGeneratorTests.cs @@ -58,6 +58,7 @@ sealed class Bar { ); AssertNewTrees( result, @"#pragma warning disable CS1572 +#nullable annotations using System; using System.IO; @@ -91,6 +92,7 @@ partial interface IFoo { ); AssertNewTrees( result, @"#pragma warning disable CS1572 +#nullable annotations using System; using System.Threading.Tasks; @@ -140,6 +142,7 @@ public async Task BarAsync() {} ); AssertNewTrees( result, @"#pragma warning disable CS1572 +#nullable annotations using System; using System.IO; @@ -153,6 +156,7 @@ partial class Bar { [Blocking] public void Baz( StreamWriter x ) { return; } }", @"#pragma warning disable CS1572 +#nullable annotations using System; using System.IO;