-
Notifications
You must be signed in to change notification settings - Fork 469
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 Do not call Enumerable.Cast<T> or Enumerable.OfType<T> false positives #6457
Comments
@buyaa-n I've started work on this |
There's one other case I think this is throwing a warning where I didn't quite expect it: [Fact]
public async Task CanCastReadonlyStructToInterface()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System.Collections;
using System.Collections.Generic;
using System.Linq;
interface IInterface {}
readonly struct Implementation : IInterface {}
class C
{
void M()
{
IEnumerable<Implementation> e = default;
e.Cast<IInterface>();
}
}
");
} The problem is It doesn't consider interfaces on structs. |
I submitted the fix PR. Its not blocking. We wanted to enable the rule in a repo that's gonna be merged into another one that has the rule enabled. This flags and breaks the build. We'll update the analyzer when it becomes available. Cc: @leculver |
Thanks @hoyosjs, the fix merged, closing this issue |
Analyzer
Diagnostic ID: CA2021:
Do not call Enumerable.Cast<T> or Enumerable.OfType<T> with incompatible types
Analyzer source
SDK: Built-in CA analyzers in .NET 8 SDK or later
Describe the bug
Following false positives found in runtime test builds (normally we don't turn on analyzers in test projects, but this analyzer
warns
by default therefore failing the test builds). So, we have lowered the analyzer severity intoinfo
level until these bugs fixed. @fowl2 please let us know if you interested in fixing these, thanks!Expected behavior
No warning
Actual behavior
Warns
Additional context
Found in: dotnet/runtime@f3bcd21
CC @fowl2 @stephentoub
The text was updated successfully, but these errors were encountered: