Open MrJul opened 11 months ago
It seems the analyzer is complaining because of the struct
constraint; Removing it removes the warning. Makes sense, as a struct type can't ever inherit from a class (except for when it can, here, and with ValueType
).
Analyzer
Diagnostic ID: CA2021 (no documentation yet):
Type 'U' is incompatible with type 'T' and cast attempts will throw InvalidCastException at runtime
Analyzer source
SDK: Built-in CA analyzers in .NET 8 SDK Version: 8.0.100
Describe the bug
CA2021 incorrectly triggers when casting
ValueType
to aT : struct
, andEnum
to aT : struct, Enum
.Steps To Reproduce
Expected behavior
No warnings.
Actual behavior
CA2021 warnings in both
Cast<T>
calls.