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 shouldn't trigger for overloads with unsatisfied notnull generic constrain #7245

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

CA2263 shouldn't trigger for overloads with unsatisfied notnull generic constrain #7245

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

Comments

@MaceWindu
Copy link

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 propose to use generic overload for type, that doesn't satisfy overload's notnull generic type

Steps To Reproduce

public static class TestClass
{
    public static T? Test1<T>(ITest1 tester)
    {
        // bad suggestion
        return (T?)tester.Test(typeof(T));

        // CS8714: The type 'T' cannot be used as type parameter 'T' in the generic type or method 'ITest.Test<T>()'.Nullability of type argument 'T' doesn't match 'notnull' constraint.
        //return tester.Test<T>();
    }

    public static T? Test2<T>(ITest1 tester)
        where T : notnull
    {
        // good suggestion
        //return (T?)tester.Test(typeof(T));

        return tester.Test<T>();
    }

    public static T? Test3<T>(ITest2 tester)
    {
        // good, no suggestion
        return (T?)tester.Test(typeof(T));

        //return tester.Test<T>();
    }

    public static T? Test4<T>(ITest2 tester)
        where T : Enum
    {
        // good suggestion
        //return (T?)tester.Test(typeof(T));

        return tester.Test<T>();
    }
}

public interface ITest1
{
    T? Test<T>() where T : notnull;

    object? Test(Type type);
}

public interface ITest2
{
    T? Test<T>() where T : Enum;

    object? Test(Type type);
}

Expected behavior

No errors when generic constrain not satisfied

Actual behavior

Proposed code change trigger compilation error

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