-
Notifications
You must be signed in to change notification settings - Fork 470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NullReferenceException from CSharpPreferGenericOverloadsAnalyzer in .NET 9 RC1 #7421
Comments
Thanks for reporting this issue, I've reproduced the problem and got the following stack trace:
You can change the line below as workaround to no longer get a NPE during build. The build then completes without errors. - Request.Relationship?.LeftType.ClrType.Should().Be(typeof(TLeft));
+ Request.Relationship?.LeftType.ClrType.Should().Be<TLeft>(); As for why a NPE exception is thrown is not completely clear to me. It happens during speculative binding of the new generic invocation: roslyn-analyzers/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/PreferGenericOverloads.cs Lines 101 to 104 in 8d4f432
I can reproduce the NPE with the following test case: [Fact, WorkItem(7421, "https://github.com/dotnet/roslyn-analyzers/issues/7421")]
public async Task Issue7421_OffersFixer_CS()
{
string source = """
#nullable enable
class A
{
public B? B { get; set; }
public C? C { get; set; }
}
class B
{
public C C { get; set; } = new();
}
class C
{
public void M(System.Type type) { }
public void M<T>() { }
void Test()
{
new A().B?.C.M(typeof(C));
}
}
""";
var test = new VerifyCS.Test
{
TestCode = source,
FixedCode = source,
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9
};
await test.RunAsync();
} This seems to have something to do with the use of a null-conditional operator and not directly calling the method on the result. In other words, if I change the line to either // Call method 'M' directly after '?.'
new A().C?.M(typeof(C)); or // Do not use '?.'
new A().B.C.M(typeof(C)); I no longer get a crash.
Based on dotnet/roslyn#72076 (comment), this could be the same problem as dotnet/roslyn#72076. |
@mpidash Thanks for investigating. I can confirm the suggested change fixes the NRE during build. |
Analyzer
Diagnostic ID: CA2263:
Prefer generic overload when type is known
Describe the bug
After updating our codebase to .NET 9 RC1, the following warning is logged during build. This doesn't show when building against .NET 8.
I'm not aware of an easy way to determine which line of code this originates from. This can be reproduced by cloning the JsonApiDotNetCore repository and building the
dotnet9-preview
branch, after commenting out the following in.editorconfig
:I've tried installing the latest prerelease version, which doesn't help:
The text was updated successfully, but these errors were encountered: