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

CA2263 (PreferGenericOverloadsAnalyzer) crashes on open generic types #7246

Closed
MaceWindu opened this issue Mar 14, 2024 · 0 comments · Fixed by #7247
Closed

CA2263 (PreferGenericOverloadsAnalyzer) crashes on open generic types #7246

MaceWindu opened this issue Mar 14, 2024 · 0 comments · Fixed by #7247

Comments

@MaceWindu
Copy link

MaceWindu commented Mar 14, 2024

Analyzer

Diagnostic ID: CA2263: Prefer generic overload when type is known

Analyzer source

NuGet Package: Microsoft.CodeAnalysis.NetAnalyzers

Version: 9.0.0-preview.24122.1

Describe the bug

CA2263 crashes for open-generic types

Steps To Reproduce

public static class TestClass
{
    public static void Test<T>(ITest tester)
    {
        // crash
        tester.Test(typeof(IEnumerable<>));
        // doesn't crash
        tester.Test(typeof(IEnumerable<string>));
    }

}

public interface ITest
{
    void Test<T>();

    // doesn't crash if commented
    void Test(string type);

    void Test(Type type, params Type[] typeArgs);
}

Expected behavior

No errors when generic constrain not satisfied

Actual behavior

Proposed code change trigger compilation error

IOperation: Invocation
1>    SyntaxNode: tester.Test(typeof(IEnumerable< ... [InvocationExpressionSyntax]@[113..147) (5,8)-(5,42)
1>    System.InvalidOperationException: Operation is not valid due to the current state of the object.
1>    at Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol.Construct(ImmutableArray`1 typeArguments)
1>    at Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.MethodSymbol.Microsoft.CodeAnalysis.IMethodSymbol.Construct(ITypeSymbol[] typeArguments)
1>    at Microsoft.NetCore.Analyzers.Usage.PreferGenericOverloadsAnalyzer.<>c__DisplayClass6_0.<AnalyzeInvocation>g__IsApplicableGenericOverload|0(IMethodSymbol method)
1>    at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
1>    at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
1>    at Microsoft.NetCore.Analyzers.Usage.PreferGenericOverloadsAnalyzer.AnalyzeInvocation(OperationAnalysisContext context)
1>    at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteOperationAction>b__53_0(ValueTuple`2 data)
1>    at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant