.net 9 ReadOnlySpan params cause issues with my analyzer #77207
-
I'm trying to update our code base from .net 8 to 9, but for some reason my analyzer is reporting code snippets that were previously fine. Basically, the analyzer checks if Task.Run(Foo); // diagnostic, returned Task is not used
_ = Task.Run(Foo); // ok, we explicitly assigned the Task Previously, the following code was fine: _ = Task.WhenAll(Task.FromResult(0), Task.FromResult(1)); but after upgrading to .net 9, the analyzer complains that both My analyzer registers for private static bool ReturnedTaskIsUsed(IOperation operation) => operation.Parent switch
{
IAwaitOperation => // await SomeTask(); await await SomeNestedTask()
!IsTaskOfTask(operation.Type!, operation.SemanticModel!.Compilation) || ReturnedTaskIsUsed(operation.Parent),
IConversionOperation
or IReturnOperation // return Task.Run(...)
or ISymbolInitializerOperation // var x = Task.Run(...)
or IAssignmentOperation // x = Task.Run(...)
or IInvocationOperation // Task.Run(...).Wait()
or IArgumentOperation // ProcessTask(Task.Run(...))
or IMemberReferenceOperation // Task.Run(...).Result
or IArrayInitializerOperation // new [] { Task.Run(...), Task.Run(...) } <---- this is where I end up in the debugger
or IConditionalOperation // x == condition ? Task.Run(...) : Task.Completed
or IConditionalAccessOperation // Task.Run(...)?.Wait()
or ITupleOperation => // (1, Task.Run(...))
true,
_ => false
}; I use this snippet to invoke the analyzer in my test: void MyTest() =>
Assert<MyAnalyzer>("""
namespace Foo
{
class Bar
{
async Task Meth()
{
_= Task.WhenAll(Task.FromResult(0), Task.FromResult(1));
}
}
}
""");
async Task Assert<T>(string source) where T : DiagnosticAnalyzer, new()
{
var test = new CSharpAnalyzerTest<T, DefaultVerifier>
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net90,
TestCode = source
};
await test.RunAsync();
} I've also updated all nuget packages to the latest version, these are (most of) the packages used in the test project: <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing" Version="1.1.2"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0"/> I've checked with the debugger and the referenced assemblies are from .net 9.0, the C# version is 13, but the operation I get for What am I missing? Why is the diagnostic reported in a normal .net 9 project but not in my analyzer test project? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
Your test probably doesn't have the right language version. I see that you've checked that, but I would double check that again. Verify that the program was parsed with C# 13, and that the compilation options are C# 13. |
Beta Was this translation helpful? Give feedback.
@sharwell this appears to be because the reference assemblies shipped with the test SDK are not the final .NET 9 assemblies: instead, they're from p1. We need to seriously consider deprecating the built-in way to control reference assemblies in the test SDK, and instead just use Basic.Reference.Assemblies.