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

CA2009 should not be raised when changing the generic type #7374

Open
andrewimcclement opened this issue Aug 13, 2024 · 0 comments
Open

CA2009 should not be raised when changing the generic type #7374

andrewimcclement opened this issue Aug 13, 2024 · 0 comments
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Positive A diagnostic is reported for non-problematic case

Comments

@andrewimcclement
Copy link

andrewimcclement commented Aug 13, 2024

Analyzer

Diagnostic ID: CA2009: Do not call ToImmutableCollection on an ImmutableCollection value

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 8.0.0

Describe the bug

CA2009 should not be raised when changing the generic type, i.e. ImmutableList<T1>.ToImmutableList<T2>() should be valid.

Steps To Reproduce

public interface IFoo {}
public class Foo : IFoo {}

public static class Program
{
    public static void Main()
    {
        var foo = new Foo();
        var list1 = ImmutableList.Create(foo);
        var list2 = list1.ToImmutableList<IFoo>(); // Generates CA2009
    }
}

Expected behavior

CA2009 should only trigger if the generic types are identical.

Actual behavior

CA2009 triggers even if the generic type is different.

Additional context

This is relevant when passing immutable collections to APIs that you do not control (obviously if I were creating list2 to pass in to a method taking IReadOnlyList<IFoo>, passing list1 directly would be fine).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers False_Positive A diagnostic is reported for non-problematic case
Projects
None yet
Development

No branches or pull requests

2 participants