-
Notifications
You must be signed in to change notification settings - Fork 467
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
CA2021 false positive for .Cast<>
from generic base class in .NET 9.0 SDK
#7357
Comments
Still a problem with Preview 7:
|
Being hit by this with the 9.0.100 RTM SDK, even on a solution targeting .NET 8. My particular variant is from a generic base class to a generic derived class, i.e. new Base<int>[] { new() }.OfType<Derived<int>>()
class Base<T> { }
class Derived<T> : Base<T> {} |
We're seeing something similar when compiling our dotnet 8 solution using dotnet 9.0.100 SDK:
We have a repo pattern where the repo fetches data from Entity Framework, and ensures the returned value is the correct type - but the signature of the repo method returns a base class:
When doing a .OfType() on the result, we get a CA2021. We also get CA2021 when we call
BaseEntity is defined in a custom NuGet package we have. Calling The whole thing works just fine when building with dotnet 8 SDK. |
The issue is more than likely that the analyzer ships as part of the SDK. |
Same here
|
Hit by this too with the .NET 9.0.100 SDK. It's a shame was not fixed before the public release. Real example that happened with MudDataGrid.RenderedColumns which is a _dataGrid?.RenderedColumns.OfType<PropertyColumn<Employee, string>>(); The code works exactly as expected but triggers CA2021. |
.Cast<>
from generic base class in .NET 9.0 Preview 6.Cast<>
from generic base class in .NET 9.0 SDK
Fix for #7357. #7183 changed the CastWillAlwaysFail(ITypeSymbol castFrom, ITypeSymbol castTo) to work with castFrom.OriginalDefinition/castTo.OriginalDefinition, eliding the generic type information in order to fix issues with interfaces, but that appears to have broken the check for class types. This reverts just the class type case to use the castFrom/castTo types passed in instead of the .OriginalDefinition. I'm not sure I quite understand why that's the only place this matters, but I tried a few variations with structs and interfaces and couldn't get any other false positives.
Analyzer
Diagnostic ID: CA2021: Type 'GenericBase' is incompatible with type 'GenericDerived' and cast attempts will throw InvalidCastException at runtime
Analyzer source
SDK: Version 9.0.100-preview.6.24238.19
Also tested with NuGet Package:
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0-preview.24318.1">
Describe the bug
NOTE: this may be a duplicate of #7153. However, since that was fixed a few months ago, I would have expected the fix to be in the latest package and latest .NET 9 Preview SDK. Opening this issue just in case it's not truly fixed.
Using
OfType<TDerived>()
orCast<TDerived>
on a collection ofTBase<T>
whereTDerived : TBase<T>
gives a spurious message about a cast that will fail at runtime, even though the cast is actually fine.Steps To Reproduce
Build the following code with .NET 9 Preview 6:
Expected behavior
Compiles without warning, as it did in .NET 8.0.
Actual behavior
Issues a spurious CA2021 warning.
The text was updated successfully, but these errors were encountered: