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

Bug: RCS1164: Unused type parameter #1107

Closed
marco-eckstein opened this issue Jul 11, 2023 · 3 comments · Fixed by #1196
Closed

Bug: RCS1164: Unused type parameter #1107

marco-eckstein opened this issue Jul 11, 2023 · 3 comments · Fixed by #1196

Comments

@marco-eckstein
Copy link

Product and Version Used:

NuGet Package Roslynator.Analyzers 4.3.0

Steps to Reproduce:

static class Demo
    {
        public static string A() =>
            Run(i => "#" + i); // Calls: Run<T>(Func<int, T>)

        public static void B() =>
            Run<Action>(_ => { }); // Calls: Run<T>(Action<int>)

        static T Run<T>(Func<int, T> block) =>
            block(1);

        static void Run<T>(Action<int> block) =>
            Run(_ => { block(2); return "ignored"; }); // Calls: Run<T>(Func<int, T>)
    }

Actual Behavior:

static void Run<T>(Action<int> block) => // RCS1164: Unused type parameter <T>

Expected Behavior:
Does not flag T as unused type parameter, because if we remove it, we get this:

static class Demo
    {
        public static string A() =>
            Run(i => "#" + i);

        public static void B() =>
            Run<Action>(_ => { }); // CS1643 Not all code paths return a value in lambda expression of type 'Func<int, Action>'


        static T Run<T>(Func<int, T> block) =>
            block(1);

        static void Run(Action<int> block) => // RCS1213: Remove unused member declaration
            Run(_ => { block(2); return "ignored"; });
    }
@josefpihrt
Copy link
Collaborator

Hi,

I agree that applying of code fix should not cause compiler error.

BUT this analyzer checks if a type parameter is actually used INSIDE the method. I can see the the type parameter is actually not used in the method in which case it makes sense to me to report it and remove it.

@seruminar
Copy link

Hi @josefpihrt, how about this example 🙂

public static class TestClass
{
    public static void RunDelegate<T>(object arg) // RCS1164: Unused type parameter <T>
    {
        RunDelegate(arg, (T arg) =>
        {
            if (arg is string stringArg)
            {
                Console.WriteLine(stringArg);
            }
        }
        );
    }

    private static void RunDelegate(
        object arg,
        Delegate _delegate
        )
    {
        _delegate.DynamicInvoke(arg);
    }
}
TestClass.RunDelegate<int>(1)
TestClass.RunDelegate<string>("prints")
prints

@josefpihrt
Copy link
Collaborator

@seruminar That seems like a bug 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants