Skip to content

Commit

Permalink
Emit a nullable directive in generated sync code (#947)
Browse files Browse the repository at this point in the history
  • Loading branch information
j3parker authored Feb 13, 2024
1 parent 785eb70 commit 5c0e2d7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/D2L.CodeStyle.Analyzers/Async/Generator/FileCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void SomeMethod() {}
);

Assert.AreEqual( @"#pragma warning disable CS1572
#nullable annotations
", collector.CollectSource() );
}

Expand Down Expand Up @@ -52,6 +53,7 @@ void MyMethodBefore() {
);

Assert.AreEqual( @"#pragma warning disable CS1572
#nullable annotations
using Foo;
Expand Down Expand Up @@ -89,6 +91,7 @@ void MyMethodBefore() {
);

Assert.AreEqual( @"#pragma warning disable CS1572
#nullable annotations
using Foo;
Expand Down Expand Up @@ -124,6 +127,7 @@ void MyMethodBefore() {{
);

Assert.AreEqual( @$"#pragma warning disable CS1572
#nullable annotations
partial {kind} X {{
any text
Expand Down Expand Up @@ -163,6 +167,7 @@ class Ignored2 {}
);

Assert.AreEqual( @"#pragma warning disable CS1572
#nullable annotations
using Foo;
Expand Down Expand Up @@ -244,6 +249,7 @@ void MyBeforeMethod4() { }
);

Assert.AreEqual( @"#pragma warning disable CS1572
#nullable annotations
using Foo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sealed class Bar {
);

AssertNewTrees( result, @"#pragma warning disable CS1572
#nullable annotations
using System;
using System.IO;
Expand Down Expand Up @@ -91,6 +92,7 @@ partial interface IFoo {
);

AssertNewTrees( result, @"#pragma warning disable CS1572
#nullable annotations
using System;
using System.Threading.Tasks;
Expand Down Expand Up @@ -140,6 +142,7 @@ public async Task BarAsync() {}
);

AssertNewTrees( result, @"#pragma warning disable CS1572
#nullable annotations
using System;
using System.IO;
Expand All @@ -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;
Expand Down

0 comments on commit 5c0e2d7

Please sign in to comment.