Skip to content
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 filtering enum of custom type inheriting from Dictionary<U, V> with OfType<T> where T is Dictionary<U, V> #7458

Open
DavidH541 opened this issue Oct 28, 2024 · 0 comments

Comments

@DavidH541
Copy link

DavidH541 commented Oct 28, 2024

Analyzer

Diagnostic ID: CA2021: Don't call Enumerable.Cast<T> or Enumerable.OfType<T> with incompatible types

Analyzer source

SDK: Built-in CA analyzers in .NET 9.0.100-rc.2.24474.11 SDK or later

Version: SDK 9.0.100-rc.2.24474.11

Describe the bug

With classes inherited from Dictionary, OfType generates a warning when filtering an enum with custom Dictionaries on a dictionary with the same types. See example below:

public class CustomDict : Dictionary<int, int>
{
    public string ExtraProp { get; set; } = string.Empty;
}

public class OfTypeWarning
{
    public IEnumerable<CustomDict> customDictEnum = [];

    public void TestOfType()
    {
        var x = customDictEnum.OfType<Dictionary<int, int>>(); <--- this generated CA2021 warning
    }
}

Expected behavior

No warnings

Actual behavior

CA2021 warning

@DavidH541 DavidH541 changed the title CA2021 False positive filtering Dictionary<T, U> with OfType<T> where T is a class inheriting from said Dictionary<T, U> CA2021 False positive filtering Dictionary<U, V> with OfType<T> where T is a class inheriting from said Dictionary<U, V> Oct 28, 2024
@DavidH541 DavidH541 changed the title CA2021 False positive filtering Dictionary<U, V> with OfType<T> where T is a class inheriting from said Dictionary<U, V> CA2021 False positive filtering enum of Dictionary<U, V> with OfType<T> where T is a class inheriting from said Dictionary<U, V> Oct 28, 2024
@DavidH541 DavidH541 changed the title CA2021 False positive filtering enum of Dictionary<U, V> with OfType<T> where T is a class inheriting from said Dictionary<U, V> CA2021 False positive filtering enum of custom type inheriting from Dictionary<U, V> with OfType<T> where T is Dictionary<U, V> Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant